Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 1e6 + 1;
int n, dp[N], b[N];
int main() {
scanf("%d", &n);
for(int i = 0, a; i < n; ++i) {
scanf("%d", &a);
scanf("%d", b + a);
}
int res = 0;
dp[0] = b[0] != 0;
for(int i = 1; i < N; ++i) {
if(b[i] == 0)
dp[i] = dp[i - 1];
else {
if(b[i] >= i)
dp[i] = 1;
else
dp[i] = dp[i - b[i] - 1] + 1;
}
res = max(res, dp[i]);
}
printf("%d\n", n - res);
return 0;
}
Copy The Code &
Try With Live Editor
Input
4
1 9
3 1
6 1
7 4
1 9
3 1
6 1
7 4
Output
1
Demonstration
Codeforces Solution-Chain Reaction-Solution in C, C++, Java, Python