This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ll ans = INF; | |
for(int color = 1; color < N-1; color++){ | |
for(int ind = 0; ind < dogs[color].size(); ind++){ | |
if(ind+1 > k) | |
break; | |
for(int taken = 0; taken <= k - ind - 1; taken++) | |
ans = min(ans, dogs[color][ind] + pre[color-1][taken] + suf[color+1][k - taken - ind - 1]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(int color = N-2; color>=0; color--){ | |
for(int taken = 0; taken<=k; taken++) | |
suf[color][taken] = INF; | |
for(int taken = 0; taken<=k; taken++){ | |
suf[color][taken] = suf[color+1][taken]; | |
for(int ind = 0; ind<dogs[color].size(); ind++){ | |
if(ind+1 > taken) | |
break; | |
suf[color][taken] = min(suf[color][taken], suf[color+1][taken-ind-1] + 2*dogs[color][ind]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Input vector<ll> dogs[N] and sort | |
//Initialise pre[][] and suf[][] with INF | |
//Initialise pre[0][0] = 0 and suf[N-1][0] = 0 | |
for(int color = 1; color<N; color++){ | |
for(int taken = 0; taken<=k; taken++){ | |
pre[color][taken] = pre[color-1][taken]; | |
for(int ind = 0; ind<dogs[color].size(); ind++){ | |
if(ind+1 > taken) | |
break; | |
pre[color][taken] = min(pre[color][taken], pre[color-1][taken-ind-1] + 2*dogs[color][ind]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int solve(int r, int c) | |
{ | |
for(int i = 0; i < r; i++) { | |
for(int j = 0; j < c; j++) { | |
int p = j+1; | |
int maxy = v[i][j]; | |
int mini = v[i][j]; | |
while(p < c) { | |
maxy = max(maxy, v[i][p]); | |
mini = min(mini, v[i][p]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map< pair<int,int>, bool > visited; | |
for (int i = 0; i < n; i++) { | |
if (s[i] == 'N') | |
while (visited[make_pair(r, c)]) | |
r--; | |
if (s[i] == 'E') | |
while (visited[make_pair(r, c)]) | |
c++; | |
if (s[i] == 'S') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Initialise dp to 0 | |
for(int i = 1; i <= n; i++) | |
{ | |
dp[i] = 0; | |
for(int j = 1; j < i; j++) | |
if(desirable[j][i]) | |
dp[i] = max(dp[i], dp[j] + 1); | |
} | |
cout << dp[n] << "\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desirable[i][j] = desirable[i+1][j] | desirable[i][j-1]; | |
if(maximum[i][j] > A[i] && maximum[i][j] > A[j]) | |
desirable[i][j] = true; | |
if(minimum[i][j] < A[i] && minimum[i][j] < A[j]) | |
desirable[i][j] = true; | |
maximum[i][j] = max(maximum[i][j-1], A[j]); |