#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1048576;
int idx[2*MAXN];
int getv(int a, int b)
{
a += MAXN; b += MAXN;
int ans = 0;
while(a<=b)
{
if(a%2==1) ans = max(ans, idx[a++]);
if(b%2==0) ans = max(ans, idx[b--]);
a/=2; b/=2;
}
return ans;
}
void setv(int a, int v)
{
idx[a+=MAXN] = v;
while((a=a/2))
idx[a] = max(idx[2*a], idx[2*a+1]);
}
void addv(int a, int v)
{
idx[a+=MAXN] += v;
while((a=a/2))
idx[a] = max(idx[2*a], idx[2*a+1]);
}
int N, K;
vector<int> adj[MAXN];
int arr[MAXN];
int anseven[MAXN], ansodd[MAXN];
void even()
{
for(int i=0; i<N/2; ++i)
{
int l = arr[2*i], r = arr[2*i+1];
if(l == r)
addv(l, 2);
else
{
addv(l, 1); addv(r, 1);
adj[l].push_back(r);
adj[r].push_back(l);
}
}
if(N%2==1) addv(arr[N-1], 1);
for(int c=1; c<=K; ++c)
{
int idxc = idx[c+MAXN]; setv(c, 0);
for(auto x: adj[c])
addv(x, -1);
anseven[c] = idxc + idx[1];
for(auto x: adj[c])
addv(x, +1);
setv(c, idxc);
}
}
void odd()
{
addv(arr[0], 1);
for(int i=0; i<(N-1)/2; ++i)
{
int l = arr[2*i+1], r = arr[2*i+2];
if(l == r)
addv(l, 2);
else
{
addv(l, 1); addv(r, 1);
adj[l].push_back(r);
adj[r].push_back(l);
}
}
if(N%2==0) addv(arr[N-1], 1);
for(int c=1; c<=K; ++c)
{
int idxc = idx[c+MAXN]; setv(c, 0);
for(auto x: adj[c])
addv(x, -1);
ansodd[c] = idxc + idx[1];
for(auto x: adj[c])
addv(x, +1);
setv(c, idxc);
}
}
int main()
{
scanf("%d%d", &N, &K);
for(int i=0; i<N; ++i) scanf("%d", arr+i);
even();
for(int i=1; i<=K; ++i) adj[i].clear();
memset(idx, 0, sizeof idx);
odd();
for(int i=1; i<=K; ++i)
printf("%d\n", N-max(anseven[i], ansodd[i]));
return 0;
}
Compilation message
rope.cpp: In function 'int main()':
rope.cpp:103:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~
rope.cpp:104:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=0; i<N; ++i) scanf("%d", arr+i);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
33144 KB |
Output is correct |
2 |
Correct |
28 ms |
33252 KB |
Output is correct |
3 |
Correct |
27 ms |
33304 KB |
Output is correct |
4 |
Correct |
27 ms |
33356 KB |
Output is correct |
5 |
Correct |
27 ms |
33356 KB |
Output is correct |
6 |
Correct |
30 ms |
33408 KB |
Output is correct |
7 |
Correct |
27 ms |
33408 KB |
Output is correct |
8 |
Correct |
27 ms |
33484 KB |
Output is correct |
9 |
Correct |
28 ms |
33556 KB |
Output is correct |
10 |
Correct |
28 ms |
33556 KB |
Output is correct |
11 |
Correct |
27 ms |
33556 KB |
Output is correct |
12 |
Correct |
32 ms |
33556 KB |
Output is correct |
13 |
Correct |
28 ms |
33556 KB |
Output is correct |
14 |
Correct |
28 ms |
33556 KB |
Output is correct |
15 |
Correct |
28 ms |
33556 KB |
Output is correct |
16 |
Correct |
28 ms |
33556 KB |
Output is correct |
17 |
Correct |
27 ms |
33556 KB |
Output is correct |
18 |
Correct |
28 ms |
33556 KB |
Output is correct |
19 |
Correct |
28 ms |
33556 KB |
Output is correct |
20 |
Correct |
28 ms |
33556 KB |
Output is correct |
21 |
Correct |
28 ms |
33556 KB |
Output is correct |
22 |
Correct |
28 ms |
33556 KB |
Output is correct |
23 |
Correct |
28 ms |
33556 KB |
Output is correct |
24 |
Correct |
28 ms |
33556 KB |
Output is correct |
25 |
Correct |
28 ms |
33556 KB |
Output is correct |
26 |
Correct |
27 ms |
33556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
33144 KB |
Output is correct |
2 |
Correct |
28 ms |
33252 KB |
Output is correct |
3 |
Correct |
27 ms |
33304 KB |
Output is correct |
4 |
Correct |
27 ms |
33356 KB |
Output is correct |
5 |
Correct |
27 ms |
33356 KB |
Output is correct |
6 |
Correct |
30 ms |
33408 KB |
Output is correct |
7 |
Correct |
27 ms |
33408 KB |
Output is correct |
8 |
Correct |
27 ms |
33484 KB |
Output is correct |
9 |
Correct |
28 ms |
33556 KB |
Output is correct |
10 |
Correct |
28 ms |
33556 KB |
Output is correct |
11 |
Correct |
27 ms |
33556 KB |
Output is correct |
12 |
Correct |
32 ms |
33556 KB |
Output is correct |
13 |
Correct |
28 ms |
33556 KB |
Output is correct |
14 |
Correct |
28 ms |
33556 KB |
Output is correct |
15 |
Correct |
28 ms |
33556 KB |
Output is correct |
16 |
Correct |
28 ms |
33556 KB |
Output is correct |
17 |
Correct |
27 ms |
33556 KB |
Output is correct |
18 |
Correct |
28 ms |
33556 KB |
Output is correct |
19 |
Correct |
28 ms |
33556 KB |
Output is correct |
20 |
Correct |
28 ms |
33556 KB |
Output is correct |
21 |
Correct |
28 ms |
33556 KB |
Output is correct |
22 |
Correct |
28 ms |
33556 KB |
Output is correct |
23 |
Correct |
28 ms |
33556 KB |
Output is correct |
24 |
Correct |
28 ms |
33556 KB |
Output is correct |
25 |
Correct |
28 ms |
33556 KB |
Output is correct |
26 |
Correct |
27 ms |
33556 KB |
Output is correct |
27 |
Correct |
81 ms |
34532 KB |
Output is correct |
28 |
Correct |
76 ms |
34532 KB |
Output is correct |
29 |
Correct |
80 ms |
34532 KB |
Output is correct |
30 |
Correct |
77 ms |
34532 KB |
Output is correct |
31 |
Correct |
82 ms |
34532 KB |
Output is correct |
32 |
Correct |
76 ms |
34532 KB |
Output is correct |
33 |
Correct |
80 ms |
34532 KB |
Output is correct |
34 |
Correct |
78 ms |
34532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
33144 KB |
Output is correct |
2 |
Correct |
28 ms |
33252 KB |
Output is correct |
3 |
Correct |
27 ms |
33304 KB |
Output is correct |
4 |
Correct |
27 ms |
33356 KB |
Output is correct |
5 |
Correct |
27 ms |
33356 KB |
Output is correct |
6 |
Correct |
30 ms |
33408 KB |
Output is correct |
7 |
Correct |
27 ms |
33408 KB |
Output is correct |
8 |
Correct |
27 ms |
33484 KB |
Output is correct |
9 |
Correct |
28 ms |
33556 KB |
Output is correct |
10 |
Correct |
28 ms |
33556 KB |
Output is correct |
11 |
Correct |
27 ms |
33556 KB |
Output is correct |
12 |
Correct |
32 ms |
33556 KB |
Output is correct |
13 |
Correct |
28 ms |
33556 KB |
Output is correct |
14 |
Correct |
28 ms |
33556 KB |
Output is correct |
15 |
Correct |
28 ms |
33556 KB |
Output is correct |
16 |
Correct |
28 ms |
33556 KB |
Output is correct |
17 |
Correct |
27 ms |
33556 KB |
Output is correct |
18 |
Correct |
28 ms |
33556 KB |
Output is correct |
19 |
Correct |
28 ms |
33556 KB |
Output is correct |
20 |
Correct |
28 ms |
33556 KB |
Output is correct |
21 |
Correct |
28 ms |
33556 KB |
Output is correct |
22 |
Correct |
28 ms |
33556 KB |
Output is correct |
23 |
Correct |
28 ms |
33556 KB |
Output is correct |
24 |
Correct |
28 ms |
33556 KB |
Output is correct |
25 |
Correct |
28 ms |
33556 KB |
Output is correct |
26 |
Correct |
27 ms |
33556 KB |
Output is correct |
27 |
Correct |
81 ms |
34532 KB |
Output is correct |
28 |
Correct |
76 ms |
34532 KB |
Output is correct |
29 |
Correct |
80 ms |
34532 KB |
Output is correct |
30 |
Correct |
77 ms |
34532 KB |
Output is correct |
31 |
Correct |
82 ms |
34532 KB |
Output is correct |
32 |
Correct |
76 ms |
34532 KB |
Output is correct |
33 |
Correct |
80 ms |
34532 KB |
Output is correct |
34 |
Correct |
78 ms |
34532 KB |
Output is correct |
35 |
Correct |
78 ms |
34532 KB |
Output is correct |
36 |
Correct |
77 ms |
34532 KB |
Output is correct |
37 |
Correct |
77 ms |
34532 KB |
Output is correct |
38 |
Correct |
77 ms |
34532 KB |
Output is correct |
39 |
Correct |
76 ms |
34532 KB |
Output is correct |
40 |
Correct |
76 ms |
34532 KB |
Output is correct |
41 |
Correct |
103 ms |
34532 KB |
Output is correct |
42 |
Correct |
77 ms |
34556 KB |
Output is correct |
43 |
Correct |
79 ms |
34556 KB |
Output is correct |
44 |
Correct |
76 ms |
34556 KB |
Output is correct |
45 |
Correct |
77 ms |
34556 KB |
Output is correct |
46 |
Correct |
77 ms |
34556 KB |
Output is correct |
47 |
Correct |
82 ms |
34616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
33144 KB |
Output is correct |
2 |
Correct |
28 ms |
33252 KB |
Output is correct |
3 |
Correct |
27 ms |
33304 KB |
Output is correct |
4 |
Correct |
27 ms |
33356 KB |
Output is correct |
5 |
Correct |
27 ms |
33356 KB |
Output is correct |
6 |
Correct |
30 ms |
33408 KB |
Output is correct |
7 |
Correct |
27 ms |
33408 KB |
Output is correct |
8 |
Correct |
27 ms |
33484 KB |
Output is correct |
9 |
Correct |
28 ms |
33556 KB |
Output is correct |
10 |
Correct |
28 ms |
33556 KB |
Output is correct |
11 |
Correct |
27 ms |
33556 KB |
Output is correct |
12 |
Correct |
32 ms |
33556 KB |
Output is correct |
13 |
Correct |
28 ms |
33556 KB |
Output is correct |
14 |
Correct |
28 ms |
33556 KB |
Output is correct |
15 |
Correct |
28 ms |
33556 KB |
Output is correct |
16 |
Correct |
28 ms |
33556 KB |
Output is correct |
17 |
Correct |
27 ms |
33556 KB |
Output is correct |
18 |
Correct |
28 ms |
33556 KB |
Output is correct |
19 |
Correct |
28 ms |
33556 KB |
Output is correct |
20 |
Correct |
28 ms |
33556 KB |
Output is correct |
21 |
Correct |
28 ms |
33556 KB |
Output is correct |
22 |
Correct |
28 ms |
33556 KB |
Output is correct |
23 |
Correct |
28 ms |
33556 KB |
Output is correct |
24 |
Correct |
28 ms |
33556 KB |
Output is correct |
25 |
Correct |
28 ms |
33556 KB |
Output is correct |
26 |
Correct |
27 ms |
33556 KB |
Output is correct |
27 |
Correct |
81 ms |
34532 KB |
Output is correct |
28 |
Correct |
76 ms |
34532 KB |
Output is correct |
29 |
Correct |
80 ms |
34532 KB |
Output is correct |
30 |
Correct |
77 ms |
34532 KB |
Output is correct |
31 |
Correct |
82 ms |
34532 KB |
Output is correct |
32 |
Correct |
76 ms |
34532 KB |
Output is correct |
33 |
Correct |
80 ms |
34532 KB |
Output is correct |
34 |
Correct |
78 ms |
34532 KB |
Output is correct |
35 |
Correct |
78 ms |
34532 KB |
Output is correct |
36 |
Correct |
77 ms |
34532 KB |
Output is correct |
37 |
Correct |
77 ms |
34532 KB |
Output is correct |
38 |
Correct |
77 ms |
34532 KB |
Output is correct |
39 |
Correct |
76 ms |
34532 KB |
Output is correct |
40 |
Correct |
76 ms |
34532 KB |
Output is correct |
41 |
Correct |
103 ms |
34532 KB |
Output is correct |
42 |
Correct |
77 ms |
34556 KB |
Output is correct |
43 |
Correct |
79 ms |
34556 KB |
Output is correct |
44 |
Correct |
76 ms |
34556 KB |
Output is correct |
45 |
Correct |
77 ms |
34556 KB |
Output is correct |
46 |
Correct |
77 ms |
34556 KB |
Output is correct |
47 |
Correct |
82 ms |
34616 KB |
Output is correct |
48 |
Correct |
524 ms |
44044 KB |
Output is correct |
49 |
Correct |
523 ms |
44044 KB |
Output is correct |
50 |
Correct |
530 ms |
44236 KB |
Output is correct |
51 |
Correct |
524 ms |
44236 KB |
Output is correct |
52 |
Correct |
510 ms |
44236 KB |
Output is correct |
53 |
Correct |
506 ms |
44236 KB |
Output is correct |
54 |
Correct |
518 ms |
44236 KB |
Output is correct |
55 |
Correct |
509 ms |
44236 KB |
Output is correct |
56 |
Correct |
502 ms |
44236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
33144 KB |
Output is correct |
2 |
Correct |
28 ms |
33252 KB |
Output is correct |
3 |
Correct |
27 ms |
33304 KB |
Output is correct |
4 |
Correct |
27 ms |
33356 KB |
Output is correct |
5 |
Correct |
27 ms |
33356 KB |
Output is correct |
6 |
Correct |
30 ms |
33408 KB |
Output is correct |
7 |
Correct |
27 ms |
33408 KB |
Output is correct |
8 |
Correct |
27 ms |
33484 KB |
Output is correct |
9 |
Correct |
28 ms |
33556 KB |
Output is correct |
10 |
Correct |
28 ms |
33556 KB |
Output is correct |
11 |
Correct |
27 ms |
33556 KB |
Output is correct |
12 |
Correct |
32 ms |
33556 KB |
Output is correct |
13 |
Correct |
28 ms |
33556 KB |
Output is correct |
14 |
Correct |
28 ms |
33556 KB |
Output is correct |
15 |
Correct |
28 ms |
33556 KB |
Output is correct |
16 |
Correct |
28 ms |
33556 KB |
Output is correct |
17 |
Correct |
27 ms |
33556 KB |
Output is correct |
18 |
Correct |
28 ms |
33556 KB |
Output is correct |
19 |
Correct |
28 ms |
33556 KB |
Output is correct |
20 |
Correct |
28 ms |
33556 KB |
Output is correct |
21 |
Correct |
28 ms |
33556 KB |
Output is correct |
22 |
Correct |
28 ms |
33556 KB |
Output is correct |
23 |
Correct |
28 ms |
33556 KB |
Output is correct |
24 |
Correct |
28 ms |
33556 KB |
Output is correct |
25 |
Correct |
28 ms |
33556 KB |
Output is correct |
26 |
Correct |
27 ms |
33556 KB |
Output is correct |
27 |
Correct |
81 ms |
34532 KB |
Output is correct |
28 |
Correct |
76 ms |
34532 KB |
Output is correct |
29 |
Correct |
80 ms |
34532 KB |
Output is correct |
30 |
Correct |
77 ms |
34532 KB |
Output is correct |
31 |
Correct |
82 ms |
34532 KB |
Output is correct |
32 |
Correct |
76 ms |
34532 KB |
Output is correct |
33 |
Correct |
80 ms |
34532 KB |
Output is correct |
34 |
Correct |
78 ms |
34532 KB |
Output is correct |
35 |
Correct |
78 ms |
34532 KB |
Output is correct |
36 |
Correct |
77 ms |
34532 KB |
Output is correct |
37 |
Correct |
77 ms |
34532 KB |
Output is correct |
38 |
Correct |
77 ms |
34532 KB |
Output is correct |
39 |
Correct |
76 ms |
34532 KB |
Output is correct |
40 |
Correct |
76 ms |
34532 KB |
Output is correct |
41 |
Correct |
103 ms |
34532 KB |
Output is correct |
42 |
Correct |
77 ms |
34556 KB |
Output is correct |
43 |
Correct |
79 ms |
34556 KB |
Output is correct |
44 |
Correct |
76 ms |
34556 KB |
Output is correct |
45 |
Correct |
77 ms |
34556 KB |
Output is correct |
46 |
Correct |
77 ms |
34556 KB |
Output is correct |
47 |
Correct |
82 ms |
34616 KB |
Output is correct |
48 |
Correct |
524 ms |
44044 KB |
Output is correct |
49 |
Correct |
523 ms |
44044 KB |
Output is correct |
50 |
Correct |
530 ms |
44236 KB |
Output is correct |
51 |
Correct |
524 ms |
44236 KB |
Output is correct |
52 |
Correct |
510 ms |
44236 KB |
Output is correct |
53 |
Correct |
506 ms |
44236 KB |
Output is correct |
54 |
Correct |
518 ms |
44236 KB |
Output is correct |
55 |
Correct |
509 ms |
44236 KB |
Output is correct |
56 |
Correct |
502 ms |
44236 KB |
Output is correct |
57 |
Correct |
2232 ms |
83504 KB |
Output is correct |
58 |
Correct |
1910 ms |
83504 KB |
Output is correct |
59 |
Correct |
1770 ms |
83504 KB |
Output is correct |
60 |
Correct |
1862 ms |
83504 KB |
Output is correct |
61 |
Correct |
1868 ms |
83504 KB |
Output is correct |
62 |
Correct |
954 ms |
83504 KB |
Output is correct |
63 |
Correct |
1384 ms |
83504 KB |
Output is correct |
64 |
Correct |
1168 ms |
83504 KB |
Output is correct |
65 |
Correct |
796 ms |
83504 KB |
Output is correct |