#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define pii pair<int,int>
#define is insert
const long long INF = 1000000000;
const long long MOD = 1000000007;
const int MAXN=4e5;
//store test cases here
/*
find longest valid subsequence,
store list of LIS's with last height
for each new member, find max previous LIS
everything geq works
everything leq
factor in distance between indices so curr height can be up to a+dist*m
when inserting in set, insert in height-ix*m
when querying for valid ones, everything geq than curr-ix*m works
handle duplicate vals-
maintain segtree, c compress values
5 400
300 700 200 1000 500
3 300
700 1000 1300
*/
int n,m;
set<int> ix;
map<int,int> cp;
int t[4*MAXN];
int dp[MAXN];
int sum(int curr,int tl,int tr,int l,int r){
if(l>r){
return 0;
}
if(l==tl&&r==tr){
return t[curr];
}
int mid=(tl+tr)/2;
return max(sum(curr*2,tl,mid,l,min(r,mid)),sum(curr*2+1,mid+1,tr,max(l,mid+1),r));
}
void update(int curr,int tl,int tr,int pos,int val){
if(tl==tr){
t[curr]=val;
}else{
int mid=(tl+tr)/2;
if(pos<=mid){
update(curr*2,tl,mid,pos,val);
}else{
update(curr*2+1,mid+1,tr,pos,val);
}
t[curr]=max(t[curr*2],t[curr*2+1]);
}
}
int a[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
ix.is(0);
//check long long
for(int i=1;i<=n;i++){
cin>>a[i];
ix.is(a[i]-i*m);
ix.is(m-i*m);
}
int tot=n;
n=2*n+5;
int ct=1;
for(auto it=ix.begin();it!=ix.end();it++){
cp[*it]=ct;
ct++;
}
int mx=0;
for(int i=1;i<=tot;i++){
dp[i]=sum(1,1,n,cp[a[i]-i*m],n)+1;
if(dp[i]==1&&a[i]-i*m>0){
//check if can't even jump from height 0
dp[i]=0;
//update(1,1,n,cp[m-i*m],1);
}else{
update(1,1,n,cp[a[i]-i*m],dp[i]);
}
mx=max(mx,dp[i]);
}
//LIS has to contain height 0
cout<<(tot-mx)<<"\n";
}
/* REMINDERS
* CHECK ARRAY BOUNDS, HOW BIG ARRAY HAS TO BE
* PLANNING!!!!!!!! Concrete plan before code
* IF CAN'T FIGURE ANYTHING OUT, MAKE TEN TEST CASES TO EVALUATE ALL TYPES OF SCENARIOS, THEN CONSTRUCT SOLUTION TO FIT IT
* IF CAN'T FIGURE ANYTHING OUT, MAKE TEN TEST CASES TO EVALUATE ALL TYPES OF SCENARIOS, THEN CONSTRUCT SOLUTION TO FIT IT
* IF CAN'T FIGURE ANYTHING OUT, MAKE TEN TEST CASES TO EVALUATE ALL TYPES OF SCENARIOS, THEN CONSTRUCT SOLUTION TO FIT IT
* NAIVE SOL FIRST TO CHECK AGAINST OPTIMIZED SOL
* MOD OUT EVERY STEP
* DON'T MAKE ASSUMPTIONS
* DON'T OVERCOMPLICATE
* CHECK INT VS LONG, IF YOU NEED TO STORE LARGE NUMBERS
* CHECK CONSTRAINTS, C <= N <= F...
* CHECK SPECIAL CASES, N = 1...
* TO TEST TLE/MLE, PLUG IN MAX VALS ALLOWED AND SEE WHAT HAPPENS
* ALSO CALCULATE BIG-O, OVERALL TIME COMPLEXITY
* IF ALL ELSE FAILS, DO CASEWORK
* compile with "g++ -std=c++11 filename.cpp" if using auto keyword
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
3 ms |
716 KB |
Output is correct |
21 |
Correct |
4 ms |
844 KB |
Output is correct |
22 |
Correct |
5 ms |
672 KB |
Output is correct |
23 |
Correct |
6 ms |
1356 KB |
Output is correct |
24 |
Correct |
5 ms |
1356 KB |
Output is correct |
25 |
Correct |
6 ms |
1356 KB |
Output is correct |
26 |
Correct |
5 ms |
1356 KB |
Output is correct |
27 |
Correct |
4 ms |
828 KB |
Output is correct |
28 |
Correct |
3 ms |
844 KB |
Output is correct |
29 |
Correct |
4 ms |
840 KB |
Output is correct |
30 |
Correct |
6 ms |
1356 KB |
Output is correct |
31 |
Correct |
4 ms |
844 KB |
Output is correct |
32 |
Correct |
5 ms |
1360 KB |
Output is correct |
33 |
Correct |
8 ms |
1356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
3 ms |
716 KB |
Output is correct |
5 |
Correct |
4 ms |
844 KB |
Output is correct |
6 |
Correct |
5 ms |
672 KB |
Output is correct |
7 |
Correct |
6 ms |
1356 KB |
Output is correct |
8 |
Correct |
5 ms |
1356 KB |
Output is correct |
9 |
Correct |
6 ms |
1356 KB |
Output is correct |
10 |
Correct |
5 ms |
1356 KB |
Output is correct |
11 |
Correct |
4 ms |
828 KB |
Output is correct |
12 |
Correct |
3 ms |
844 KB |
Output is correct |
13 |
Correct |
4 ms |
840 KB |
Output is correct |
14 |
Correct |
6 ms |
1356 KB |
Output is correct |
15 |
Correct |
4 ms |
844 KB |
Output is correct |
16 |
Correct |
5 ms |
1360 KB |
Output is correct |
17 |
Correct |
8 ms |
1356 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
316 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
0 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
4 ms |
848 KB |
Output is correct |
35 |
Correct |
6 ms |
1332 KB |
Output is correct |
36 |
Correct |
6 ms |
1356 KB |
Output is correct |
37 |
Correct |
6 ms |
1436 KB |
Output is correct |
38 |
Correct |
4 ms |
836 KB |
Output is correct |
39 |
Correct |
4 ms |
896 KB |
Output is correct |
40 |
Correct |
4 ms |
812 KB |
Output is correct |
41 |
Correct |
3 ms |
844 KB |
Output is correct |
42 |
Correct |
6 ms |
1356 KB |
Output is correct |
43 |
Correct |
6 ms |
1356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
848 KB |
Output is correct |
2 |
Correct |
6 ms |
1332 KB |
Output is correct |
3 |
Correct |
6 ms |
1356 KB |
Output is correct |
4 |
Correct |
6 ms |
1436 KB |
Output is correct |
5 |
Correct |
4 ms |
836 KB |
Output is correct |
6 |
Correct |
4 ms |
896 KB |
Output is correct |
7 |
Correct |
4 ms |
812 KB |
Output is correct |
8 |
Correct |
3 ms |
844 KB |
Output is correct |
9 |
Correct |
6 ms |
1356 KB |
Output is correct |
10 |
Correct |
6 ms |
1356 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
3 ms |
716 KB |
Output is correct |
15 |
Correct |
4 ms |
844 KB |
Output is correct |
16 |
Correct |
5 ms |
672 KB |
Output is correct |
17 |
Correct |
6 ms |
1356 KB |
Output is correct |
18 |
Correct |
5 ms |
1356 KB |
Output is correct |
19 |
Correct |
6 ms |
1356 KB |
Output is correct |
20 |
Correct |
5 ms |
1356 KB |
Output is correct |
21 |
Correct |
4 ms |
828 KB |
Output is correct |
22 |
Correct |
3 ms |
844 KB |
Output is correct |
23 |
Correct |
4 ms |
840 KB |
Output is correct |
24 |
Correct |
6 ms |
1356 KB |
Output is correct |
25 |
Correct |
4 ms |
844 KB |
Output is correct |
26 |
Correct |
5 ms |
1360 KB |
Output is correct |
27 |
Correct |
8 ms |
1356 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
316 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
0 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
332 KB |
Output is correct |
41 |
Correct |
0 ms |
332 KB |
Output is correct |
42 |
Correct |
0 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
205 ms |
23052 KB |
Output is correct |
45 |
Correct |
323 ms |
22588 KB |
Output is correct |
46 |
Correct |
435 ms |
44480 KB |
Output is correct |
47 |
Correct |
509 ms |
44460 KB |
Output is correct |
48 |
Correct |
266 ms |
42956 KB |
Output is correct |
49 |
Correct |
284 ms |
44600 KB |
Output is correct |
50 |
Correct |
193 ms |
22652 KB |
Output is correct |
51 |
Correct |
216 ms |
22548 KB |
Output is correct |
52 |
Correct |
199 ms |
22532 KB |
Output is correct |
53 |
Correct |
149 ms |
21896 KB |
Output is correct |
54 |
Correct |
272 ms |
44440 KB |
Output is correct |
55 |
Correct |
285 ms |
44340 KB |
Output is correct |