Repository: luctivud/All-Of-Competitive-Programming
Branch: master
Commit: 0a8926089af6
Files: 37
Total size: 115.4 KB
Directory structure:
gitextract_peww_frl/
├── 00-Setting-up-local-environment/
│ └── README.md
├── 01-Material/
│ └── README.md
├── 02-Codes/
│ ├── Editorials/
│ │ ├── ConvertToStrictlyIncreasing/
│ │ │ ├── ConevertToStrictlyIncreasing.md
│ │ │ ├── convertToIncreasing.cpp
│ │ │ └── convertToIncreasing.py
│ │ └── Random/
│ │ └── TransposeColorsWork.cpp
│ ├── README.md
│ └── Snippets/
│ ├── Raw/
│ │ └── 166_e_MatMul_w_Ashishgup.cpp
│ ├── Sublime/
│ │ ├── FastReadWrite.sublime-snippet
│ │ ├── addundirectededge.sublime-snippet
│ │ ├── basic-template.sublime-snippet
│ │ ├── check execution time.sublime-snippet
│ │ ├── checkvalid.sublime-snippet
│ │ ├── cmp-priority.sublime-snippet
│ │ ├── common-completions.sublime-completions
│ │ ├── dsu-array.sublime-snippet
│ │ ├── dsu.sublime-snippet
│ │ ├── errorWatch.sublime-snippet
│ │ ├── eulerTotientFunc.sublime-snippet
│ │ ├── leetcode-solution-class.sublime-snippet
│ │ ├── manipulated-sieve-of-eratosthene.sublime-snippet
│ │ ├── matrix-expo.sublime-snippet
│ │ ├── max-biparite-matching.sublime-snippet
│ │ ├── modop.sublime-snippet
│ │ ├── myReadTemplate.sublime-snippet
│ │ ├── ncr-on-demand.sublime-snippet
│ │ ├── pbds.sublime-snippet
│ │ ├── power-expo.sublime-snippet
│ │ ├── sieve-of-eratosthene.sublime-snippet
│ │ └── topcoder-template.sublime-snippet
│ └── mydebug.h
├── 03-Interview Preparation/
│ ├── Docs/
│ │ ├── .gitkeep
│ │ ├── 0x01_Interview_Preparation_in_C_v1.0.docx
│ │ └── System Design Guide.docx
│ ├── PDF/
│ │ └── .gitkeep
│ └── README.md
└── README.md
================================================
FILE CONTENTS
================================================
================================================
FILE: 00-Setting-up-local-environment/README.md
================================================
# Setting up Competitive Programming on Local Machine :
#### Content
- [Benefits](#Benefits)
- [SetUp](#Setting-up-env)
- [Snippets](#Snippets)
1. After solving a few examples on pen and paper, we get to see that a greedy solution would fail on cases like [8, 1, 2, 3, 4] or [7, 1, 5, 2, 4, 8, 6]. 2. Another key observation is that in the transformed array which is strictly increasing, we will find that there is atleast one element which remains unchanged.
DP transition states: - **dp[j] = min(dp[j-1], dp[j] + abs(A[i] - B[j]))** ∀ i, j ∈ [1, N]. Please note that the array **A** here is the converted array represented by (Ai-i) ∀ i ∈ [1, N] and **B** is its sorted version. Our final answer that represents the minimum cost is stored in the final index of our **dp** array which represents the optimal array till last index of **A**.
If our minimum cost is not more than **K** then we can certainly achieve the optimal solution. Note : This dp solution can be made more state verbose by storing the brute forced optimal substructure into their respective states in dp[i][j] but I prefer it this way.```python N = int(input()) K = int(input()) A = [0] + list(map(int, input().split())) # Convert A[i] = A[i] - i for i in range(1, N+1): A[i] = A[i] - i # store sorted A in B to stay working # with best possible element already B = [0] + sorted(A[1:]) # dp array dp = [0 for x in range(N+1)] # unreachable state dp[0] = float('inf') for i in range(1, N+1): for j in range(1, N+1): dp[j] = min(dp[j-1], dp[j] + abs(A[i] - B[j])) # dp[N] represent the ans print("YES "+str(dp[N]) if dp[N] <= K else "NO") ```
```cpp
#include
Check the subfolders for contents, it has some snippets and editorials
================================================ FILE: 02-Codes/Snippets/Raw/166_e_MatMul_w_Ashishgup.cpp ================================================ // J A I S H R E E R A M // #include