답안 #837488

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
837488 2023-08-25T11:20:53 Z _martynas Rope (JOI17_rope) C++11
100 / 100
873 ms 73844 KB
#include <bits/stdc++.h>

using namespace std;

const int mxn = 1e6+5;

int n, m;
int a[mxn], ans[mxn];
int mxcnt = 0;
int cnt[mxn], revcnt[mxn];
vector<vector<int>> where;

void add(int x) {
    if(cnt[x] == mxcnt) mxcnt++;
    revcnt[cnt[x]]--;
    cnt[x]++;
    revcnt[cnt[x]]++;
}

void rem(int x) {
    if(cnt[x] == mxcnt && revcnt[cnt[x]] == 1) mxcnt--;
    revcnt[cnt[x]]--;
    cnt[x]--;
    revcnt[cnt[x]]++;
}

int main(int argc, char const *argv[]) {
    cin >> n >> m;
    where.resize(m+1);
    for(int i = 0; i < n; i++) cin >> a[i], where[a[i]].push_back(i);
    for(int i = 1; i <= m; i++) ans[i] = n;
    int curr = n;
    for(int i = 0; i < n; i++) add(a[i]);
    for(int skipfirst : {1, 0}) {
        for(int i = 1; i <= m; i++) {
            for(int j : where[i]) {
                curr--; rem(a[j]);
                if(j % 2 == skipfirst) {
                    // from left
                    if(j == n-1) continue; // not part of a pair
                    if(a[j+1] != i) rem(a[j+1]);
                }
                else {
                    // from right
                    if(j == 0) continue; // not part of a pair
                    if(a[j-1] != i) rem(a[j-1]);
                }
            }
            ans[i] = min(ans[i], curr-mxcnt);
            for(int j : where[i]) {
                curr++; add(a[j]);
                if(j % 2 == skipfirst) {
                    // from left
                    if(j == n-1) continue; // not part of a pair
                    if(a[j+1] != i) add(a[j+1]);
                }
                else {
                    // from right
                    if(j == 0) continue; // not part of a pair
                    if(a[j-1] != i) add(a[j-1]);
                }
            }
        }
    }
    for(int i = 1; i <= m; i++) {
        cout << ans[i] << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 16 ms 1364 KB Output is correct
28 Correct 17 ms 1368 KB Output is correct
29 Correct 16 ms 1364 KB Output is correct
30 Correct 17 ms 1396 KB Output is correct
31 Correct 16 ms 1360 KB Output is correct
32 Correct 17 ms 1400 KB Output is correct
33 Correct 16 ms 1236 KB Output is correct
34 Correct 16 ms 1444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 16 ms 1364 KB Output is correct
28 Correct 17 ms 1368 KB Output is correct
29 Correct 16 ms 1364 KB Output is correct
30 Correct 17 ms 1396 KB Output is correct
31 Correct 16 ms 1360 KB Output is correct
32 Correct 17 ms 1400 KB Output is correct
33 Correct 16 ms 1236 KB Output is correct
34 Correct 16 ms 1444 KB Output is correct
35 Correct 22 ms 1324 KB Output is correct
36 Correct 21 ms 1324 KB Output is correct
37 Correct 25 ms 1296 KB Output is correct
38 Correct 20 ms 1236 KB Output is correct
39 Correct 20 ms 1224 KB Output is correct
40 Correct 20 ms 1364 KB Output is correct
41 Correct 19 ms 1388 KB Output is correct
42 Correct 19 ms 1368 KB Output is correct
43 Correct 19 ms 1336 KB Output is correct
44 Correct 19 ms 1364 KB Output is correct
45 Correct 19 ms 1364 KB Output is correct
46 Correct 19 ms 1336 KB Output is correct
47 Correct 19 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 16 ms 1364 KB Output is correct
28 Correct 17 ms 1368 KB Output is correct
29 Correct 16 ms 1364 KB Output is correct
30 Correct 17 ms 1396 KB Output is correct
31 Correct 16 ms 1360 KB Output is correct
32 Correct 17 ms 1400 KB Output is correct
33 Correct 16 ms 1236 KB Output is correct
34 Correct 16 ms 1444 KB Output is correct
35 Correct 22 ms 1324 KB Output is correct
36 Correct 21 ms 1324 KB Output is correct
37 Correct 25 ms 1296 KB Output is correct
38 Correct 20 ms 1236 KB Output is correct
39 Correct 20 ms 1224 KB Output is correct
40 Correct 20 ms 1364 KB Output is correct
41 Correct 19 ms 1388 KB Output is correct
42 Correct 19 ms 1368 KB Output is correct
43 Correct 19 ms 1336 KB Output is correct
44 Correct 19 ms 1364 KB Output is correct
45 Correct 19 ms 1364 KB Output is correct
46 Correct 19 ms 1336 KB Output is correct
47 Correct 19 ms 1372 KB Output is correct
48 Correct 269 ms 10976 KB Output is correct
49 Correct 238 ms 10876 KB Output is correct
50 Correct 227 ms 11048 KB Output is correct
51 Correct 215 ms 10960 KB Output is correct
52 Correct 197 ms 9904 KB Output is correct
53 Correct 218 ms 10128 KB Output is correct
54 Correct 203 ms 9136 KB Output is correct
55 Correct 209 ms 8948 KB Output is correct
56 Correct 207 ms 8772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 16 ms 1364 KB Output is correct
28 Correct 17 ms 1368 KB Output is correct
29 Correct 16 ms 1364 KB Output is correct
30 Correct 17 ms 1396 KB Output is correct
31 Correct 16 ms 1360 KB Output is correct
32 Correct 17 ms 1400 KB Output is correct
33 Correct 16 ms 1236 KB Output is correct
34 Correct 16 ms 1444 KB Output is correct
35 Correct 22 ms 1324 KB Output is correct
36 Correct 21 ms 1324 KB Output is correct
37 Correct 25 ms 1296 KB Output is correct
38 Correct 20 ms 1236 KB Output is correct
39 Correct 20 ms 1224 KB Output is correct
40 Correct 20 ms 1364 KB Output is correct
41 Correct 19 ms 1388 KB Output is correct
42 Correct 19 ms 1368 KB Output is correct
43 Correct 19 ms 1336 KB Output is correct
44 Correct 19 ms 1364 KB Output is correct
45 Correct 19 ms 1364 KB Output is correct
46 Correct 19 ms 1336 KB Output is correct
47 Correct 19 ms 1372 KB Output is correct
48 Correct 269 ms 10976 KB Output is correct
49 Correct 238 ms 10876 KB Output is correct
50 Correct 227 ms 11048 KB Output is correct
51 Correct 215 ms 10960 KB Output is correct
52 Correct 197 ms 9904 KB Output is correct
53 Correct 218 ms 10128 KB Output is correct
54 Correct 203 ms 9136 KB Output is correct
55 Correct 209 ms 8948 KB Output is correct
56 Correct 207 ms 8772 KB Output is correct
57 Correct 873 ms 73844 KB Output is correct
58 Correct 724 ms 46064 KB Output is correct
59 Correct 663 ms 45896 KB Output is correct
60 Correct 731 ms 52868 KB Output is correct
61 Correct 675 ms 52992 KB Output is correct
62 Correct 368 ms 28088 KB Output is correct
63 Correct 468 ms 36100 KB Output is correct
64 Correct 425 ms 31136 KB Output is correct
65 Correct 302 ms 18908 KB Output is correct