#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) (int(x.size()))
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
struct przedzialowiec{
int N;
vector<multiset<int>> sety;
vector<int> t;
przedzialowiec(int n){
for(N = 1; N <= n;) N <<= 1;
sety.resize(n+1);
t.resize(N<<1, 0);
}
void napraw(int poz){
t[poz+N-1] = sety[poz].empty() ? 0 : *prev(sety[poz].end());
for(poz+=N-1; poz>>=1;) t[poz] = max(t[poz<<1], t[poz<<1|1]);
}
void dorzuc(int poz, int wart){
sety[poz].emplace(wart);
napraw(poz);
}
void wywal(int poz, int wart){
sety[poz].erase(sety[poz].find(wart));
napraw(poz);
}
int maks(int p, int k){
int ret = 0;
for(p+=N-1, k+=N; p<k; p>>=1, k>>=1){
if(p&1) ret = max(ret, t[p++]);
if(k&1) ret = max(ret, t[--k]);
}
return ret;
}
};
int main(){
int n, d;
scanf("%d%d", &n, &d);
set<int> wart;
vector<int> wej(n+1);
FOR(i, 1, n) scanf("%d", &wej[i]), wart.emplace(wej[i]);
unordered_map<int, int> mapa;
for(int it = 0; int x : wart) mapa[x] = ++it;
FOR(i, 1, n) wej[i] = mapa[wej[i]];
vector<vector<int>> kub(n+1);
FOR(i, 1, n) kub[wej[i]].emplace_back(i);
vector<int> prawo(n+1);
set<int> duze_grupy;
set<pii> grupy;
for(int x = n+1; --x;){
for(int i : kub[x]){
auto it = duze_grupy.upper_bound(i);
prawo[i] = it==duze_grupy.end() ? n : (*it)+d-1;
}
for(int i : kub[x]){
int gdzie = i, ile = 1;
auto it = grupy.lower_bound({i+1, 0});
if(it != grupy.end() && it->fi == i+1){
if(it->se >= d) duze_grupy.erase(it->fi);
ile += it->se, it = grupy.erase(it);
}
if(it != grupy.begin() && prev(it)->fi + prev(it)->se == i){
if(prev(it)->se >= d) duze_grupy.erase(prev(it)->fi);
gdzie = prev(it)->fi, ile += prev(it)->se, grupy.erase(prev(it));
}
grupy.emplace(gdzie, ile);
if(ile >= d) duze_grupy.emplace(gdzie);
}
}
vector<int> dp(n+1, 0);
przedzialowiec prz(n);
vector<vector<pii>> akt(n+1);
FOR(i, 1, n){
dp[i] = 1 + prz.maks(1, wej[i]-1);
prz.dorzuc(wej[i], dp[i]);
akt[prawo[i]].emplace_back(wej[i], dp[i]);
for(pii para : akt[i]) prz.wywal(para.fi, para.se);
}
int wyn = 0;
FOR(i, 1, n) if(prawo[i] == n) wyn = max(wyn, dp[i]);
printf("%d", wyn);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:49:18: warning: range-based 'for' loops with initializer only available with '-std=c++2a' or '-std=gnu++2a'
49 | for(int it = 0; int x : wart) mapa[x] = ++it;
| ^~~
Main.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d%d", &n, &d);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:47:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | FOR(i, 1, n) scanf("%d", &wej[i]), wart.emplace(wej[i]);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
7 ms |
1372 KB |
Output is correct |
42 |
Correct |
8 ms |
2052 KB |
Output is correct |
43 |
Correct |
4 ms |
1372 KB |
Output is correct |
44 |
Correct |
5 ms |
1880 KB |
Output is correct |
45 |
Correct |
6 ms |
1884 KB |
Output is correct |
46 |
Correct |
7 ms |
2396 KB |
Output is correct |
47 |
Correct |
6 ms |
2140 KB |
Output is correct |
48 |
Correct |
7 ms |
2140 KB |
Output is correct |
49 |
Correct |
7 ms |
2140 KB |
Output is correct |
50 |
Correct |
6 ms |
2140 KB |
Output is correct |
51 |
Correct |
7 ms |
2140 KB |
Output is correct |
52 |
Correct |
8 ms |
2140 KB |
Output is correct |
53 |
Correct |
5 ms |
2140 KB |
Output is correct |
54 |
Correct |
5 ms |
2140 KB |
Output is correct |
55 |
Correct |
6 ms |
2396 KB |
Output is correct |
56 |
Correct |
6 ms |
2424 KB |
Output is correct |
57 |
Correct |
8 ms |
2396 KB |
Output is correct |
58 |
Correct |
6 ms |
2396 KB |
Output is correct |
59 |
Correct |
5 ms |
2276 KB |
Output is correct |
60 |
Correct |
5 ms |
2396 KB |
Output is correct |
61 |
Correct |
6 ms |
2368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
197 ms |
55220 KB |
Output is correct |
2 |
Correct |
234 ms |
47200 KB |
Output is correct |
3 |
Correct |
297 ms |
46644 KB |
Output is correct |
4 |
Correct |
651 ms |
75820 KB |
Output is correct |
5 |
Correct |
478 ms |
81104 KB |
Output is correct |
6 |
Correct |
657 ms |
80608 KB |
Output is correct |
7 |
Correct |
220 ms |
83484 KB |
Output is correct |
8 |
Correct |
219 ms |
92544 KB |
Output is correct |
9 |
Correct |
246 ms |
81492 KB |
Output is correct |
10 |
Correct |
273 ms |
86252 KB |
Output is correct |
11 |
Correct |
471 ms |
80160 KB |
Output is correct |
12 |
Correct |
518 ms |
80256 KB |
Output is correct |
13 |
Correct |
479 ms |
84096 KB |
Output is correct |
14 |
Correct |
570 ms |
77440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
182 ms |
53908 KB |
Output is correct |
2 |
Correct |
250 ms |
57692 KB |
Output is correct |
3 |
Correct |
329 ms |
58180 KB |
Output is correct |
4 |
Correct |
564 ms |
91512 KB |
Output is correct |
5 |
Correct |
459 ms |
93312 KB |
Output is correct |
6 |
Correct |
641 ms |
92704 KB |
Output is correct |
7 |
Correct |
228 ms |
92140 KB |
Output is correct |
8 |
Correct |
249 ms |
93308 KB |
Output is correct |
9 |
Correct |
277 ms |
92128 KB |
Output is correct |
10 |
Correct |
426 ms |
91628 KB |
Output is correct |
11 |
Correct |
492 ms |
91440 KB |
Output is correct |
12 |
Correct |
517 ms |
92256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
7 ms |
1372 KB |
Output is correct |
42 |
Correct |
8 ms |
2052 KB |
Output is correct |
43 |
Correct |
4 ms |
1372 KB |
Output is correct |
44 |
Correct |
5 ms |
1880 KB |
Output is correct |
45 |
Correct |
6 ms |
1884 KB |
Output is correct |
46 |
Correct |
7 ms |
2396 KB |
Output is correct |
47 |
Correct |
6 ms |
2140 KB |
Output is correct |
48 |
Correct |
7 ms |
2140 KB |
Output is correct |
49 |
Correct |
7 ms |
2140 KB |
Output is correct |
50 |
Correct |
6 ms |
2140 KB |
Output is correct |
51 |
Correct |
7 ms |
2140 KB |
Output is correct |
52 |
Correct |
8 ms |
2140 KB |
Output is correct |
53 |
Correct |
5 ms |
2140 KB |
Output is correct |
54 |
Correct |
5 ms |
2140 KB |
Output is correct |
55 |
Correct |
6 ms |
2396 KB |
Output is correct |
56 |
Correct |
6 ms |
2424 KB |
Output is correct |
57 |
Correct |
8 ms |
2396 KB |
Output is correct |
58 |
Correct |
6 ms |
2396 KB |
Output is correct |
59 |
Correct |
5 ms |
2276 KB |
Output is correct |
60 |
Correct |
5 ms |
2396 KB |
Output is correct |
61 |
Correct |
6 ms |
2368 KB |
Output is correct |
62 |
Correct |
197 ms |
55220 KB |
Output is correct |
63 |
Correct |
234 ms |
47200 KB |
Output is correct |
64 |
Correct |
297 ms |
46644 KB |
Output is correct |
65 |
Correct |
651 ms |
75820 KB |
Output is correct |
66 |
Correct |
478 ms |
81104 KB |
Output is correct |
67 |
Correct |
657 ms |
80608 KB |
Output is correct |
68 |
Correct |
220 ms |
83484 KB |
Output is correct |
69 |
Correct |
219 ms |
92544 KB |
Output is correct |
70 |
Correct |
246 ms |
81492 KB |
Output is correct |
71 |
Correct |
273 ms |
86252 KB |
Output is correct |
72 |
Correct |
471 ms |
80160 KB |
Output is correct |
73 |
Correct |
518 ms |
80256 KB |
Output is correct |
74 |
Correct |
479 ms |
84096 KB |
Output is correct |
75 |
Correct |
570 ms |
77440 KB |
Output is correct |
76 |
Correct |
182 ms |
53908 KB |
Output is correct |
77 |
Correct |
250 ms |
57692 KB |
Output is correct |
78 |
Correct |
329 ms |
58180 KB |
Output is correct |
79 |
Correct |
564 ms |
91512 KB |
Output is correct |
80 |
Correct |
459 ms |
93312 KB |
Output is correct |
81 |
Correct |
641 ms |
92704 KB |
Output is correct |
82 |
Correct |
228 ms |
92140 KB |
Output is correct |
83 |
Correct |
249 ms |
93308 KB |
Output is correct |
84 |
Correct |
277 ms |
92128 KB |
Output is correct |
85 |
Correct |
426 ms |
91628 KB |
Output is correct |
86 |
Correct |
492 ms |
91440 KB |
Output is correct |
87 |
Correct |
517 ms |
92256 KB |
Output is correct |
88 |
Correct |
349 ms |
46196 KB |
Output is correct |
89 |
Correct |
681 ms |
78172 KB |
Output is correct |
90 |
Correct |
354 ms |
53084 KB |
Output is correct |
91 |
Correct |
670 ms |
93312 KB |
Output is correct |
92 |
Correct |
235 ms |
57900 KB |
Output is correct |
93 |
Correct |
341 ms |
59656 KB |
Output is correct |
94 |
Correct |
551 ms |
83036 KB |
Output is correct |
95 |
Correct |
564 ms |
80508 KB |
Output is correct |
96 |
Correct |
630 ms |
79932 KB |
Output is correct |
97 |
Correct |
655 ms |
82552 KB |
Output is correct |
98 |
Correct |
700 ms |
91660 KB |
Output is correct |
99 |
Correct |
663 ms |
92796 KB |
Output is correct |
100 |
Correct |
679 ms |
92328 KB |
Output is correct |
101 |
Correct |
261 ms |
79996 KB |
Output is correct |
102 |
Correct |
293 ms |
78380 KB |
Output is correct |
103 |
Correct |
364 ms |
78108 KB |
Output is correct |
104 |
Correct |
410 ms |
78448 KB |
Output is correct |
105 |
Correct |
475 ms |
82812 KB |
Output is correct |
106 |
Correct |
396 ms |
91828 KB |
Output is correct |
107 |
Correct |
467 ms |
91444 KB |
Output is correct |
108 |
Correct |
580 ms |
91596 KB |
Output is correct |
109 |
Correct |
377 ms |
84096 KB |
Output is correct |
110 |
Correct |
625 ms |
87736 KB |
Output is correct |
111 |
Correct |
555 ms |
89216 KB |
Output is correct |
112 |
Correct |
379 ms |
89728 KB |
Output is correct |
113 |
Correct |
569 ms |
93112 KB |
Output is correct |
114 |
Correct |
566 ms |
91776 KB |
Output is correct |
115 |
Correct |
261 ms |
91772 KB |
Output is correct |
116 |
Correct |
251 ms |
92796 KB |
Output is correct |
117 |
Correct |
275 ms |
92672 KB |
Output is correct |
118 |
Correct |
278 ms |
91260 KB |
Output is correct |
119 |
Correct |
337 ms |
92096 KB |
Output is correct |
120 |
Correct |
370 ms |
91424 KB |
Output is correct |