/*
5 400
300
700
200
1000
500
3 300
700
1000
1300
*/
import java.util.*;
import java.io.*;
public class triusis{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer details = new StringTokenizer(br.readLine());
int n = Integer.parseInt(details.nextToken()); //# of towers
int m = Integer.parseInt(details.nextToken()); //amount that Carrot can jump
long[] towers = new long[n + 1];
TreeMap<Long, Integer> ind = new TreeMap<Long, Integer>(); //for compression of tower heights, as they can be too large to store in the segment tree
int add = 0;
for(int a = 1; a <= n; a++){
towers[a] = Long.parseLong(br.readLine()) - (a)*m; //normalizes array, don't have to account for Carrot's jumps anymore, just find N - (longest non increasing subsequence)
ind.put(towers[a], 0);
}
ind.put(0L,0); //dummy value
n++;
int ct = 0;
for(long item : ind.keySet()) ind.put(item, ct++);
//System.out.println("ind " + ind);
//System.out.println("towers " + Arrays.toString(towers));
//create a new pole of height 0 at beginning of list
int[] dp = new int[n]; //dp[i] = longest decreasing subsequence starting at i (i is the largest #)
segTree max = new segTree(n, dp);
//int mx = 0;
for(int a = n-1; a >= 0; a--){
dp[a] = max.q(0,ind.get(towers[a])) + 1;
max.u(ind.get(towers[a]), dp[a]);
//mx = Math.max(mx, dp[a]);
}
//System.out.println("dp " + Arrays.toString(dp));
System.out.println(n - dp[0]);
br.close();
}
}
class segTree{
public int N;
public int[] st; //st = new int[4N/N << 2]
public int[] vals; //initial values
public segTree(int n, int[] vs){
N = n;
vals = vs.clone();
st = new int[n * 4];
build(1, 0, N - 1);
}
public int left(int p){
return 2 * p;
}
public int right(int p){
return 2 * p + 1;
}
public int join(int a, int b){
//take children, update value
//can change this function to change from maximum to sum/minimum
return Math.max(a, b);
}
public void build(int p, int l, int r){
if(l == r) st[p] = vals[l];
else{
build(left(p), l, (l + r)/2);
build(right(p), (l + r)/2 + 1, r);
st[p] = join(st[left(p)], st[right(p)]);
}
}
public void update(int p, int l, int r, int index, int value){
if(index < l || index > r) return;
if(l == r) st[p] = Math.max(st[p], value); //in the case of this problem. in case there are duplicate values
else{
update(left(p), l, (l + r)/2, index, value);
update(right(p), (l + r)/2 + 1, r, index, value);
st[p] = join(st[left(p)], st[right(p)]);
}
}
public int query(int p, int l, int r, int x, int y){
if(x > r || y < l) return 0; //change this to large number/return -1 to calculate minimum, if child == -1 dont include
if(x <= l && y >= r) return st[p];
return join(query(left(p), l, (l + r)/2, x, y), query(right(p), (l + r)/2 + 1, r, x, y));
}
public int q(int left, int right){
return query(1, 0, N - 1, left, right);
}
public void u(int index, int value){
update(1, 0, N - 1, index, value);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
8716 KB |
Output is correct |
2 |
Correct |
60 ms |
8376 KB |
Output is correct |
3 |
Correct |
58 ms |
8540 KB |
Output is correct |
4 |
Correct |
60 ms |
8408 KB |
Output is correct |
5 |
Correct |
65 ms |
8464 KB |
Output is correct |
6 |
Correct |
60 ms |
8472 KB |
Output is correct |
7 |
Correct |
59 ms |
8268 KB |
Output is correct |
8 |
Correct |
60 ms |
8572 KB |
Output is correct |
9 |
Correct |
63 ms |
8356 KB |
Output is correct |
10 |
Correct |
61 ms |
8352 KB |
Output is correct |
11 |
Correct |
61 ms |
8372 KB |
Output is correct |
12 |
Correct |
60 ms |
8460 KB |
Output is correct |
13 |
Correct |
60 ms |
8544 KB |
Output is correct |
14 |
Correct |
61 ms |
8264 KB |
Output is correct |
15 |
Correct |
59 ms |
8360 KB |
Output is correct |
16 |
Correct |
61 ms |
8660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
8716 KB |
Output is correct |
2 |
Correct |
60 ms |
8376 KB |
Output is correct |
3 |
Correct |
58 ms |
8540 KB |
Output is correct |
4 |
Correct |
60 ms |
8408 KB |
Output is correct |
5 |
Correct |
65 ms |
8464 KB |
Output is correct |
6 |
Correct |
60 ms |
8472 KB |
Output is correct |
7 |
Correct |
59 ms |
8268 KB |
Output is correct |
8 |
Correct |
60 ms |
8572 KB |
Output is correct |
9 |
Correct |
63 ms |
8356 KB |
Output is correct |
10 |
Correct |
61 ms |
8352 KB |
Output is correct |
11 |
Correct |
61 ms |
8372 KB |
Output is correct |
12 |
Correct |
60 ms |
8460 KB |
Output is correct |
13 |
Correct |
60 ms |
8544 KB |
Output is correct |
14 |
Correct |
61 ms |
8264 KB |
Output is correct |
15 |
Correct |
59 ms |
8360 KB |
Output is correct |
16 |
Correct |
61 ms |
8660 KB |
Output is correct |
17 |
Correct |
62 ms |
8492 KB |
Output is correct |
18 |
Correct |
60 ms |
8388 KB |
Output is correct |
19 |
Correct |
63 ms |
8600 KB |
Output is correct |
20 |
Correct |
162 ms |
11284 KB |
Output is correct |
21 |
Correct |
193 ms |
11568 KB |
Output is correct |
22 |
Correct |
203 ms |
12560 KB |
Output is correct |
23 |
Correct |
238 ms |
13016 KB |
Output is correct |
24 |
Correct |
239 ms |
13028 KB |
Output is correct |
25 |
Correct |
207 ms |
12480 KB |
Output is correct |
26 |
Correct |
233 ms |
13120 KB |
Output is correct |
27 |
Correct |
186 ms |
11808 KB |
Output is correct |
28 |
Correct |
188 ms |
12132 KB |
Output is correct |
29 |
Correct |
189 ms |
11528 KB |
Output is correct |
30 |
Correct |
207 ms |
12908 KB |
Output is correct |
31 |
Correct |
248 ms |
12544 KB |
Output is correct |
32 |
Correct |
230 ms |
12860 KB |
Output is correct |
33 |
Correct |
232 ms |
12776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
8492 KB |
Output is correct |
2 |
Correct |
60 ms |
8388 KB |
Output is correct |
3 |
Correct |
63 ms |
8600 KB |
Output is correct |
4 |
Correct |
162 ms |
11284 KB |
Output is correct |
5 |
Correct |
193 ms |
11568 KB |
Output is correct |
6 |
Correct |
203 ms |
12560 KB |
Output is correct |
7 |
Correct |
238 ms |
13016 KB |
Output is correct |
8 |
Correct |
239 ms |
13028 KB |
Output is correct |
9 |
Correct |
207 ms |
12480 KB |
Output is correct |
10 |
Correct |
233 ms |
13120 KB |
Output is correct |
11 |
Correct |
186 ms |
11808 KB |
Output is correct |
12 |
Correct |
188 ms |
12132 KB |
Output is correct |
13 |
Correct |
189 ms |
11528 KB |
Output is correct |
14 |
Correct |
207 ms |
12908 KB |
Output is correct |
15 |
Correct |
248 ms |
12544 KB |
Output is correct |
16 |
Correct |
230 ms |
12860 KB |
Output is correct |
17 |
Correct |
232 ms |
12776 KB |
Output is correct |
18 |
Correct |
60 ms |
8716 KB |
Output is correct |
19 |
Correct |
60 ms |
8376 KB |
Output is correct |
20 |
Correct |
58 ms |
8540 KB |
Output is correct |
21 |
Correct |
60 ms |
8408 KB |
Output is correct |
22 |
Correct |
65 ms |
8464 KB |
Output is correct |
23 |
Correct |
60 ms |
8472 KB |
Output is correct |
24 |
Correct |
59 ms |
8268 KB |
Output is correct |
25 |
Correct |
60 ms |
8572 KB |
Output is correct |
26 |
Correct |
63 ms |
8356 KB |
Output is correct |
27 |
Correct |
61 ms |
8352 KB |
Output is correct |
28 |
Correct |
61 ms |
8372 KB |
Output is correct |
29 |
Correct |
60 ms |
8460 KB |
Output is correct |
30 |
Correct |
60 ms |
8544 KB |
Output is correct |
31 |
Correct |
61 ms |
8264 KB |
Output is correct |
32 |
Correct |
59 ms |
8360 KB |
Output is correct |
33 |
Correct |
61 ms |
8660 KB |
Output is correct |
34 |
Correct |
236 ms |
12972 KB |
Output is correct |
35 |
Correct |
240 ms |
12776 KB |
Output is correct |
36 |
Correct |
236 ms |
13148 KB |
Output is correct |
37 |
Correct |
209 ms |
12576 KB |
Output is correct |
38 |
Correct |
180 ms |
11892 KB |
Output is correct |
39 |
Correct |
184 ms |
12040 KB |
Output is correct |
40 |
Correct |
244 ms |
12832 KB |
Output is correct |
41 |
Correct |
181 ms |
12104 KB |
Output is correct |
42 |
Correct |
231 ms |
12468 KB |
Output is correct |
43 |
Correct |
211 ms |
12916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
236 ms |
12972 KB |
Output is correct |
2 |
Correct |
240 ms |
12776 KB |
Output is correct |
3 |
Correct |
236 ms |
13148 KB |
Output is correct |
4 |
Correct |
209 ms |
12576 KB |
Output is correct |
5 |
Correct |
180 ms |
11892 KB |
Output is correct |
6 |
Correct |
184 ms |
12040 KB |
Output is correct |
7 |
Correct |
244 ms |
12832 KB |
Output is correct |
8 |
Correct |
181 ms |
12104 KB |
Output is correct |
9 |
Correct |
231 ms |
12468 KB |
Output is correct |
10 |
Correct |
211 ms |
12916 KB |
Output is correct |
11 |
Correct |
62 ms |
8492 KB |
Output is correct |
12 |
Correct |
60 ms |
8388 KB |
Output is correct |
13 |
Correct |
63 ms |
8600 KB |
Output is correct |
14 |
Correct |
162 ms |
11284 KB |
Output is correct |
15 |
Correct |
193 ms |
11568 KB |
Output is correct |
16 |
Correct |
203 ms |
12560 KB |
Output is correct |
17 |
Correct |
238 ms |
13016 KB |
Output is correct |
18 |
Correct |
239 ms |
13028 KB |
Output is correct |
19 |
Correct |
207 ms |
12480 KB |
Output is correct |
20 |
Correct |
233 ms |
13120 KB |
Output is correct |
21 |
Correct |
186 ms |
11808 KB |
Output is correct |
22 |
Correct |
188 ms |
12132 KB |
Output is correct |
23 |
Correct |
189 ms |
11528 KB |
Output is correct |
24 |
Correct |
207 ms |
12908 KB |
Output is correct |
25 |
Correct |
248 ms |
12544 KB |
Output is correct |
26 |
Correct |
230 ms |
12860 KB |
Output is correct |
27 |
Correct |
232 ms |
12776 KB |
Output is correct |
28 |
Correct |
60 ms |
8716 KB |
Output is correct |
29 |
Correct |
60 ms |
8376 KB |
Output is correct |
30 |
Correct |
58 ms |
8540 KB |
Output is correct |
31 |
Correct |
60 ms |
8408 KB |
Output is correct |
32 |
Correct |
65 ms |
8464 KB |
Output is correct |
33 |
Correct |
60 ms |
8472 KB |
Output is correct |
34 |
Correct |
59 ms |
8268 KB |
Output is correct |
35 |
Correct |
60 ms |
8572 KB |
Output is correct |
36 |
Correct |
63 ms |
8356 KB |
Output is correct |
37 |
Correct |
61 ms |
8352 KB |
Output is correct |
38 |
Correct |
61 ms |
8372 KB |
Output is correct |
39 |
Correct |
60 ms |
8460 KB |
Output is correct |
40 |
Correct |
60 ms |
8544 KB |
Output is correct |
41 |
Correct |
61 ms |
8264 KB |
Output is correct |
42 |
Correct |
59 ms |
8360 KB |
Output is correct |
43 |
Correct |
61 ms |
8660 KB |
Output is correct |
44 |
Correct |
536 ms |
42432 KB |
Output is correct |
45 |
Correct |
861 ms |
45480 KB |
Output is correct |
46 |
Correct |
788 ms |
45452 KB |
Output is correct |
47 |
Correct |
786 ms |
45500 KB |
Output is correct |
48 |
Correct |
501 ms |
42612 KB |
Output is correct |
49 |
Correct |
496 ms |
43564 KB |
Output is correct |
50 |
Correct |
340 ms |
20640 KB |
Output is correct |
51 |
Correct |
319 ms |
20464 KB |
Output is correct |
52 |
Correct |
513 ms |
34572 KB |
Output is correct |
53 |
Correct |
296 ms |
19828 KB |
Output is correct |
54 |
Correct |
515 ms |
43224 KB |
Output is correct |
55 |
Correct |
517 ms |
43112 KB |
Output is correct |