#include <iostream>
#include <vector>
#define MAXN 1000005
using namespace std;
int N, M, C[MAXN], Val[MAXN][2], Ans[MAXN];
vector <int> Pairs[MAXN][2];
class segtree {
int ST[MAXN * 4];
public:
void upd(int ind, int val, int l, int r, int cur) {
if (ind < l || ind > r) {return;}
if (l == r) {
ST[cur] += val;
} else {
int mid = (l + r) >> 1;
upd(ind, val, l, mid, 2*cur);
upd(ind, val, mid+1, r, 2*cur+1);
ST[cur] = max(ST[2*cur], ST[2*cur+1]);
}
}
int get() {return(ST[1]);}
} ST1, ST2;
int main() {
//freopen("ropein.txt","r",stdin);
scanf("%d %d",&N,&M);
for (int i=1; i<=M; i++) {
Ans[i] = N;
}
for (int i=1; i<=N; i++) {
scanf("%d",&C[i]);
Ans[C[i]] -= 1;
}
for (int i=1; i<N; i+=2) {
Pairs[C[i]][0].push_back(C[i+1]);
Pairs[C[i+1]][0].push_back(C[i]);
Val[C[i]][0]++, Val[C[i+1]][0]++;
}
for (int i=2; i<N; i+=2) {
Pairs[C[i]][1].push_back(C[i+1]);
Pairs[C[i+1]][1].push_back(C[i]);
Val[C[i]][1]++, Val[C[i+1]][1]++;
}
if (N%2) {
Val[C[N]][0]++, Val[C[1]][1]++;
} else {
Val[C[1]][1]++, Val[C[N]][1]++;
}
for (int i=1; i<=M; i++) {
ST1.upd(i, Val[i][0], 1, M, 1);
ST2.upd(i, Val[i][1], 1, M, 1);
//printf("%d %d %d\n",i,Val[i][0], Val[i][1]);
}
for (int i=1; i<=M; i++) {
//solve case 1
for (int v: Pairs[i][0]) {
ST1.upd(v, -1, 1, M, 1);
}
ST1.upd(i, -1<<20, 1, M, 1);
Ans[i] = min(Ans[i], N - ST1.get() - Val[i][0]);
for (int v: Pairs[i][0]) {
ST1.upd(v, 1, 1, M, 1);
}
ST1.upd(i, 1<<20, 1, M, 1);
//solve case 2:
for (int v: Pairs[i][1]) {
ST2.upd(v, -1, 1, M, 1);
}
ST2.upd(i, -1<<20, 1, M, 1);
Ans[i] = min(Ans[i], N - ST2.get() - Val[i][1]);
for (int v: Pairs[i][1]) {
ST2.upd(v, 1, 1, M, 1);
}
ST2.upd(i, 1<<20, 1, M, 1);
}
for (int i=1; i<=M; i++) {
printf("%d\n",Ans[i]);
}
}
Compilation message
rope.cpp: In function 'int main()':
rope.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
27 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
rope.cpp:32:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
32 | scanf("%d",&C[i]);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
47360 KB |
Output is correct |
2 |
Correct |
30 ms |
47352 KB |
Output is correct |
3 |
Correct |
30 ms |
47352 KB |
Output is correct |
4 |
Correct |
30 ms |
47352 KB |
Output is correct |
5 |
Correct |
30 ms |
47352 KB |
Output is correct |
6 |
Correct |
35 ms |
47352 KB |
Output is correct |
7 |
Correct |
29 ms |
47360 KB |
Output is correct |
8 |
Correct |
31 ms |
47360 KB |
Output is correct |
9 |
Correct |
30 ms |
47360 KB |
Output is correct |
10 |
Correct |
30 ms |
47328 KB |
Output is correct |
11 |
Correct |
29 ms |
47360 KB |
Output is correct |
12 |
Correct |
31 ms |
47480 KB |
Output is correct |
13 |
Correct |
30 ms |
47352 KB |
Output is correct |
14 |
Correct |
30 ms |
47352 KB |
Output is correct |
15 |
Correct |
31 ms |
47360 KB |
Output is correct |
16 |
Correct |
30 ms |
47360 KB |
Output is correct |
17 |
Correct |
32 ms |
47360 KB |
Output is correct |
18 |
Correct |
31 ms |
47380 KB |
Output is correct |
19 |
Correct |
30 ms |
47352 KB |
Output is correct |
20 |
Correct |
43 ms |
47368 KB |
Output is correct |
21 |
Correct |
31 ms |
47356 KB |
Output is correct |
22 |
Correct |
30 ms |
47360 KB |
Output is correct |
23 |
Correct |
29 ms |
47352 KB |
Output is correct |
24 |
Correct |
32 ms |
47352 KB |
Output is correct |
25 |
Correct |
30 ms |
47352 KB |
Output is correct |
26 |
Correct |
30 ms |
47356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
47360 KB |
Output is correct |
2 |
Correct |
30 ms |
47352 KB |
Output is correct |
3 |
Correct |
30 ms |
47352 KB |
Output is correct |
4 |
Correct |
30 ms |
47352 KB |
Output is correct |
5 |
Correct |
30 ms |
47352 KB |
Output is correct |
6 |
Correct |
35 ms |
47352 KB |
Output is correct |
7 |
Correct |
29 ms |
47360 KB |
Output is correct |
8 |
Correct |
31 ms |
47360 KB |
Output is correct |
9 |
Correct |
30 ms |
47360 KB |
Output is correct |
10 |
Correct |
30 ms |
47328 KB |
Output is correct |
11 |
Correct |
29 ms |
47360 KB |
Output is correct |
12 |
Correct |
31 ms |
47480 KB |
Output is correct |
13 |
Correct |
30 ms |
47352 KB |
Output is correct |
14 |
Correct |
30 ms |
47352 KB |
Output is correct |
15 |
Correct |
31 ms |
47360 KB |
Output is correct |
16 |
Correct |
30 ms |
47360 KB |
Output is correct |
17 |
Correct |
32 ms |
47360 KB |
Output is correct |
18 |
Correct |
31 ms |
47380 KB |
Output is correct |
19 |
Correct |
30 ms |
47352 KB |
Output is correct |
20 |
Correct |
43 ms |
47368 KB |
Output is correct |
21 |
Correct |
31 ms |
47356 KB |
Output is correct |
22 |
Correct |
30 ms |
47360 KB |
Output is correct |
23 |
Correct |
29 ms |
47352 KB |
Output is correct |
24 |
Correct |
32 ms |
47352 KB |
Output is correct |
25 |
Correct |
30 ms |
47352 KB |
Output is correct |
26 |
Correct |
30 ms |
47356 KB |
Output is correct |
27 |
Correct |
56 ms |
49060 KB |
Output is correct |
28 |
Correct |
53 ms |
48888 KB |
Output is correct |
29 |
Correct |
57 ms |
49144 KB |
Output is correct |
30 |
Correct |
51 ms |
48980 KB |
Output is correct |
31 |
Correct |
67 ms |
49144 KB |
Output is correct |
32 |
Correct |
49 ms |
48956 KB |
Output is correct |
33 |
Correct |
62 ms |
49016 KB |
Output is correct |
34 |
Correct |
52 ms |
49144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
47360 KB |
Output is correct |
2 |
Correct |
30 ms |
47352 KB |
Output is correct |
3 |
Correct |
30 ms |
47352 KB |
Output is correct |
4 |
Correct |
30 ms |
47352 KB |
Output is correct |
5 |
Correct |
30 ms |
47352 KB |
Output is correct |
6 |
Correct |
35 ms |
47352 KB |
Output is correct |
7 |
Correct |
29 ms |
47360 KB |
Output is correct |
8 |
Correct |
31 ms |
47360 KB |
Output is correct |
9 |
Correct |
30 ms |
47360 KB |
Output is correct |
10 |
Correct |
30 ms |
47328 KB |
Output is correct |
11 |
Correct |
29 ms |
47360 KB |
Output is correct |
12 |
Correct |
31 ms |
47480 KB |
Output is correct |
13 |
Correct |
30 ms |
47352 KB |
Output is correct |
14 |
Correct |
30 ms |
47352 KB |
Output is correct |
15 |
Correct |
31 ms |
47360 KB |
Output is correct |
16 |
Correct |
30 ms |
47360 KB |
Output is correct |
17 |
Correct |
32 ms |
47360 KB |
Output is correct |
18 |
Correct |
31 ms |
47380 KB |
Output is correct |
19 |
Correct |
30 ms |
47352 KB |
Output is correct |
20 |
Correct |
43 ms |
47368 KB |
Output is correct |
21 |
Correct |
31 ms |
47356 KB |
Output is correct |
22 |
Correct |
30 ms |
47360 KB |
Output is correct |
23 |
Correct |
29 ms |
47352 KB |
Output is correct |
24 |
Correct |
32 ms |
47352 KB |
Output is correct |
25 |
Correct |
30 ms |
47352 KB |
Output is correct |
26 |
Correct |
30 ms |
47356 KB |
Output is correct |
27 |
Correct |
56 ms |
49060 KB |
Output is correct |
28 |
Correct |
53 ms |
48888 KB |
Output is correct |
29 |
Correct |
57 ms |
49144 KB |
Output is correct |
30 |
Correct |
51 ms |
48980 KB |
Output is correct |
31 |
Correct |
67 ms |
49144 KB |
Output is correct |
32 |
Correct |
49 ms |
48956 KB |
Output is correct |
33 |
Correct |
62 ms |
49016 KB |
Output is correct |
34 |
Correct |
52 ms |
49144 KB |
Output is correct |
35 |
Correct |
81 ms |
49148 KB |
Output is correct |
36 |
Correct |
80 ms |
49144 KB |
Output is correct |
37 |
Correct |
80 ms |
49144 KB |
Output is correct |
38 |
Correct |
93 ms |
49144 KB |
Output is correct |
39 |
Correct |
82 ms |
49144 KB |
Output is correct |
40 |
Correct |
72 ms |
49400 KB |
Output is correct |
41 |
Correct |
73 ms |
49400 KB |
Output is correct |
42 |
Correct |
69 ms |
49528 KB |
Output is correct |
43 |
Correct |
70 ms |
49400 KB |
Output is correct |
44 |
Correct |
75 ms |
49400 KB |
Output is correct |
45 |
Correct |
72 ms |
49528 KB |
Output is correct |
46 |
Correct |
70 ms |
49272 KB |
Output is correct |
47 |
Correct |
70 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
47360 KB |
Output is correct |
2 |
Correct |
30 ms |
47352 KB |
Output is correct |
3 |
Correct |
30 ms |
47352 KB |
Output is correct |
4 |
Correct |
30 ms |
47352 KB |
Output is correct |
5 |
Correct |
30 ms |
47352 KB |
Output is correct |
6 |
Correct |
35 ms |
47352 KB |
Output is correct |
7 |
Correct |
29 ms |
47360 KB |
Output is correct |
8 |
Correct |
31 ms |
47360 KB |
Output is correct |
9 |
Correct |
30 ms |
47360 KB |
Output is correct |
10 |
Correct |
30 ms |
47328 KB |
Output is correct |
11 |
Correct |
29 ms |
47360 KB |
Output is correct |
12 |
Correct |
31 ms |
47480 KB |
Output is correct |
13 |
Correct |
30 ms |
47352 KB |
Output is correct |
14 |
Correct |
30 ms |
47352 KB |
Output is correct |
15 |
Correct |
31 ms |
47360 KB |
Output is correct |
16 |
Correct |
30 ms |
47360 KB |
Output is correct |
17 |
Correct |
32 ms |
47360 KB |
Output is correct |
18 |
Correct |
31 ms |
47380 KB |
Output is correct |
19 |
Correct |
30 ms |
47352 KB |
Output is correct |
20 |
Correct |
43 ms |
47368 KB |
Output is correct |
21 |
Correct |
31 ms |
47356 KB |
Output is correct |
22 |
Correct |
30 ms |
47360 KB |
Output is correct |
23 |
Correct |
29 ms |
47352 KB |
Output is correct |
24 |
Correct |
32 ms |
47352 KB |
Output is correct |
25 |
Correct |
30 ms |
47352 KB |
Output is correct |
26 |
Correct |
30 ms |
47356 KB |
Output is correct |
27 |
Correct |
56 ms |
49060 KB |
Output is correct |
28 |
Correct |
53 ms |
48888 KB |
Output is correct |
29 |
Correct |
57 ms |
49144 KB |
Output is correct |
30 |
Correct |
51 ms |
48980 KB |
Output is correct |
31 |
Correct |
67 ms |
49144 KB |
Output is correct |
32 |
Correct |
49 ms |
48956 KB |
Output is correct |
33 |
Correct |
62 ms |
49016 KB |
Output is correct |
34 |
Correct |
52 ms |
49144 KB |
Output is correct |
35 |
Correct |
81 ms |
49148 KB |
Output is correct |
36 |
Correct |
80 ms |
49144 KB |
Output is correct |
37 |
Correct |
80 ms |
49144 KB |
Output is correct |
38 |
Correct |
93 ms |
49144 KB |
Output is correct |
39 |
Correct |
82 ms |
49144 KB |
Output is correct |
40 |
Correct |
72 ms |
49400 KB |
Output is correct |
41 |
Correct |
73 ms |
49400 KB |
Output is correct |
42 |
Correct |
69 ms |
49528 KB |
Output is correct |
43 |
Correct |
70 ms |
49400 KB |
Output is correct |
44 |
Correct |
75 ms |
49400 KB |
Output is correct |
45 |
Correct |
72 ms |
49528 KB |
Output is correct |
46 |
Correct |
70 ms |
49272 KB |
Output is correct |
47 |
Correct |
70 ms |
49400 KB |
Output is correct |
48 |
Correct |
677 ms |
69112 KB |
Output is correct |
49 |
Correct |
678 ms |
68984 KB |
Output is correct |
50 |
Correct |
677 ms |
68984 KB |
Output is correct |
51 |
Correct |
614 ms |
68728 KB |
Output is correct |
52 |
Correct |
576 ms |
66004 KB |
Output is correct |
53 |
Correct |
602 ms |
67576 KB |
Output is correct |
54 |
Correct |
577 ms |
65528 KB |
Output is correct |
55 |
Correct |
580 ms |
65144 KB |
Output is correct |
56 |
Correct |
578 ms |
64504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
47360 KB |
Output is correct |
2 |
Correct |
30 ms |
47352 KB |
Output is correct |
3 |
Correct |
30 ms |
47352 KB |
Output is correct |
4 |
Correct |
30 ms |
47352 KB |
Output is correct |
5 |
Correct |
30 ms |
47352 KB |
Output is correct |
6 |
Correct |
35 ms |
47352 KB |
Output is correct |
7 |
Correct |
29 ms |
47360 KB |
Output is correct |
8 |
Correct |
31 ms |
47360 KB |
Output is correct |
9 |
Correct |
30 ms |
47360 KB |
Output is correct |
10 |
Correct |
30 ms |
47328 KB |
Output is correct |
11 |
Correct |
29 ms |
47360 KB |
Output is correct |
12 |
Correct |
31 ms |
47480 KB |
Output is correct |
13 |
Correct |
30 ms |
47352 KB |
Output is correct |
14 |
Correct |
30 ms |
47352 KB |
Output is correct |
15 |
Correct |
31 ms |
47360 KB |
Output is correct |
16 |
Correct |
30 ms |
47360 KB |
Output is correct |
17 |
Correct |
32 ms |
47360 KB |
Output is correct |
18 |
Correct |
31 ms |
47380 KB |
Output is correct |
19 |
Correct |
30 ms |
47352 KB |
Output is correct |
20 |
Correct |
43 ms |
47368 KB |
Output is correct |
21 |
Correct |
31 ms |
47356 KB |
Output is correct |
22 |
Correct |
30 ms |
47360 KB |
Output is correct |
23 |
Correct |
29 ms |
47352 KB |
Output is correct |
24 |
Correct |
32 ms |
47352 KB |
Output is correct |
25 |
Correct |
30 ms |
47352 KB |
Output is correct |
26 |
Correct |
30 ms |
47356 KB |
Output is correct |
27 |
Correct |
56 ms |
49060 KB |
Output is correct |
28 |
Correct |
53 ms |
48888 KB |
Output is correct |
29 |
Correct |
57 ms |
49144 KB |
Output is correct |
30 |
Correct |
51 ms |
48980 KB |
Output is correct |
31 |
Correct |
67 ms |
49144 KB |
Output is correct |
32 |
Correct |
49 ms |
48956 KB |
Output is correct |
33 |
Correct |
62 ms |
49016 KB |
Output is correct |
34 |
Correct |
52 ms |
49144 KB |
Output is correct |
35 |
Correct |
81 ms |
49148 KB |
Output is correct |
36 |
Correct |
80 ms |
49144 KB |
Output is correct |
37 |
Correct |
80 ms |
49144 KB |
Output is correct |
38 |
Correct |
93 ms |
49144 KB |
Output is correct |
39 |
Correct |
82 ms |
49144 KB |
Output is correct |
40 |
Correct |
72 ms |
49400 KB |
Output is correct |
41 |
Correct |
73 ms |
49400 KB |
Output is correct |
42 |
Correct |
69 ms |
49528 KB |
Output is correct |
43 |
Correct |
70 ms |
49400 KB |
Output is correct |
44 |
Correct |
75 ms |
49400 KB |
Output is correct |
45 |
Correct |
72 ms |
49528 KB |
Output is correct |
46 |
Correct |
70 ms |
49272 KB |
Output is correct |
47 |
Correct |
70 ms |
49400 KB |
Output is correct |
48 |
Correct |
677 ms |
69112 KB |
Output is correct |
49 |
Correct |
678 ms |
68984 KB |
Output is correct |
50 |
Correct |
677 ms |
68984 KB |
Output is correct |
51 |
Correct |
614 ms |
68728 KB |
Output is correct |
52 |
Correct |
576 ms |
66004 KB |
Output is correct |
53 |
Correct |
602 ms |
67576 KB |
Output is correct |
54 |
Correct |
577 ms |
65528 KB |
Output is correct |
55 |
Correct |
580 ms |
65144 KB |
Output is correct |
56 |
Correct |
578 ms |
64504 KB |
Output is correct |
57 |
Execution timed out |
2548 ms |
148888 KB |
Time limit exceeded |
58 |
Halted |
0 ms |
0 KB |
- |