Submission #397587

# Submission time Handle Problem Language Result Execution time Memory
397587 2021-05-02T12:38:18 Z timmyfeng Rope (JOI17_rope) C++17
100 / 100
1301 ms 198184 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1000001;

int color[N], tally[N], ans[N];
vector<int> pairs[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; ++i) {
        cin >> color[i];
        ++tally[color[i]];
    }

    priority_queue<array<int, 2>> maxi;
    for (int i = 1; i <= m; ++i) {
        maxi.push({tally[i], i});
        ans[i] = INT_MAX;
    }

    for (int k = 0; k < 2; ++k) {
        fill(pairs, pairs + m + 1, vector<int>());
        for (int i = k; i + 2 <= n; i += 2) {
            if (color[i] != color[i + 1]) {
                pairs[color[i]].push_back(color[i + 1]);
                pairs[color[i + 1]].push_back(color[i]);
            }
        }
 
        for (int i = 1; i <= m; ++i) {
            int temp = tally[i];
            maxi.push({0, i});
            tally[i] = 0;

            for (auto j : pairs[i]) {
                maxi.push({--tally[j], j});
            }

            while (maxi.top()[0] != tally[maxi.top()[1]]) {
                maxi.pop();
            }
 
            ans[i] = min(ans[i], n - temp - maxi.top()[0]);
 
            for (auto j : pairs[i]) {
                maxi.push({++tally[j], j});
            }

            tally[i] = temp;
            maxi.push({tally[i], i});
        }
    }

    for (int i = 1; i <= m; ++i) {
        cout << ans[i] << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23756 KB Output is correct
2 Correct 16 ms 23748 KB Output is correct
3 Correct 16 ms 23756 KB Output is correct
4 Correct 16 ms 23756 KB Output is correct
5 Correct 16 ms 23756 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 15 ms 23824 KB Output is correct
8 Correct 16 ms 23744 KB Output is correct
9 Correct 16 ms 23756 KB Output is correct
10 Correct 15 ms 23816 KB Output is correct
11 Correct 15 ms 23752 KB Output is correct
12 Correct 15 ms 23756 KB Output is correct
13 Correct 16 ms 23756 KB Output is correct
14 Correct 16 ms 23756 KB Output is correct
15 Correct 15 ms 23792 KB Output is correct
16 Correct 16 ms 23788 KB Output is correct
17 Correct 16 ms 23804 KB Output is correct
18 Correct 15 ms 23756 KB Output is correct
19 Correct 15 ms 23708 KB Output is correct
20 Correct 15 ms 23696 KB Output is correct
21 Correct 15 ms 23756 KB Output is correct
22 Correct 16 ms 23760 KB Output is correct
23 Correct 16 ms 23696 KB Output is correct
24 Correct 16 ms 23776 KB Output is correct
25 Correct 15 ms 23756 KB Output is correct
26 Correct 15 ms 23712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23756 KB Output is correct
2 Correct 16 ms 23748 KB Output is correct
3 Correct 16 ms 23756 KB Output is correct
4 Correct 16 ms 23756 KB Output is correct
5 Correct 16 ms 23756 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 15 ms 23824 KB Output is correct
8 Correct 16 ms 23744 KB Output is correct
9 Correct 16 ms 23756 KB Output is correct
10 Correct 15 ms 23816 KB Output is correct
11 Correct 15 ms 23752 KB Output is correct
12 Correct 15 ms 23756 KB Output is correct
13 Correct 16 ms 23756 KB Output is correct
14 Correct 16 ms 23756 KB Output is correct
15 Correct 15 ms 23792 KB Output is correct
16 Correct 16 ms 23788 KB Output is correct
17 Correct 16 ms 23804 KB Output is correct
18 Correct 15 ms 23756 KB Output is correct
19 Correct 15 ms 23708 KB Output is correct
20 Correct 15 ms 23696 KB Output is correct
21 Correct 15 ms 23756 KB Output is correct
22 Correct 16 ms 23760 KB Output is correct
23 Correct 16 ms 23696 KB Output is correct
24 Correct 16 ms 23776 KB Output is correct
25 Correct 15 ms 23756 KB Output is correct
26 Correct 15 ms 23712 KB Output is correct
27 Correct 75 ms 25416 KB Output is correct
28 Correct 69 ms 25188 KB Output is correct
29 Correct 75 ms 25328 KB Output is correct
30 Correct 67 ms 25260 KB Output is correct
31 Correct 73 ms 25352 KB Output is correct
32 Correct 62 ms 25240 KB Output is correct
33 Correct 73 ms 25300 KB Output is correct
34 Correct 67 ms 25180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23756 KB Output is correct
2 Correct 16 ms 23748 KB Output is correct
3 Correct 16 ms 23756 KB Output is correct
4 Correct 16 ms 23756 KB Output is correct
5 Correct 16 ms 23756 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 15 ms 23824 KB Output is correct
8 Correct 16 ms 23744 KB Output is correct
9 Correct 16 ms 23756 KB Output is correct
10 Correct 15 ms 23816 KB Output is correct
11 Correct 15 ms 23752 KB Output is correct
12 Correct 15 ms 23756 KB Output is correct
13 Correct 16 ms 23756 KB Output is correct
14 Correct 16 ms 23756 KB Output is correct
15 Correct 15 ms 23792 KB Output is correct
16 Correct 16 ms 23788 KB Output is correct
17 Correct 16 ms 23804 KB Output is correct
18 Correct 15 ms 23756 KB Output is correct
19 Correct 15 ms 23708 KB Output is correct
20 Correct 15 ms 23696 KB Output is correct
21 Correct 15 ms 23756 KB Output is correct
22 Correct 16 ms 23760 KB Output is correct
23 Correct 16 ms 23696 KB Output is correct
24 Correct 16 ms 23776 KB Output is correct
25 Correct 15 ms 23756 KB Output is correct
26 Correct 15 ms 23712 KB Output is correct
27 Correct 75 ms 25416 KB Output is correct
28 Correct 69 ms 25188 KB Output is correct
29 Correct 75 ms 25328 KB Output is correct
30 Correct 67 ms 25260 KB Output is correct
31 Correct 73 ms 25352 KB Output is correct
32 Correct 62 ms 25240 KB Output is correct
33 Correct 73 ms 25300 KB Output is correct
34 Correct 67 ms 25180 KB Output is correct
35 Correct 40 ms 29096 KB Output is correct
36 Correct 40 ms 29008 KB Output is correct
37 Correct 42 ms 28912 KB Output is correct
38 Correct 39 ms 28928 KB Output is correct
39 Correct 39 ms 28976 KB Output is correct
40 Correct 68 ms 26968 KB Output is correct
41 Correct 68 ms 27000 KB Output is correct
42 Correct 72 ms 25932 KB Output is correct
43 Correct 70 ms 27012 KB Output is correct
44 Correct 52 ms 29052 KB Output is correct
45 Correct 56 ms 29116 KB Output is correct
46 Correct 59 ms 26984 KB Output is correct
47 Correct 55 ms 29032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23756 KB Output is correct
2 Correct 16 ms 23748 KB Output is correct
3 Correct 16 ms 23756 KB Output is correct
4 Correct 16 ms 23756 KB Output is correct
5 Correct 16 ms 23756 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 15 ms 23824 KB Output is correct
8 Correct 16 ms 23744 KB Output is correct
9 Correct 16 ms 23756 KB Output is correct
10 Correct 15 ms 23816 KB Output is correct
11 Correct 15 ms 23752 KB Output is correct
12 Correct 15 ms 23756 KB Output is correct
13 Correct 16 ms 23756 KB Output is correct
14 Correct 16 ms 23756 KB Output is correct
15 Correct 15 ms 23792 KB Output is correct
16 Correct 16 ms 23788 KB Output is correct
17 Correct 16 ms 23804 KB Output is correct
18 Correct 15 ms 23756 KB Output is correct
19 Correct 15 ms 23708 KB Output is correct
20 Correct 15 ms 23696 KB Output is correct
21 Correct 15 ms 23756 KB Output is correct
22 Correct 16 ms 23760 KB Output is correct
23 Correct 16 ms 23696 KB Output is correct
24 Correct 16 ms 23776 KB Output is correct
25 Correct 15 ms 23756 KB Output is correct
26 Correct 15 ms 23712 KB Output is correct
27 Correct 75 ms 25416 KB Output is correct
28 Correct 69 ms 25188 KB Output is correct
29 Correct 75 ms 25328 KB Output is correct
30 Correct 67 ms 25260 KB Output is correct
31 Correct 73 ms 25352 KB Output is correct
32 Correct 62 ms 25240 KB Output is correct
33 Correct 73 ms 25300 KB Output is correct
34 Correct 67 ms 25180 KB Output is correct
35 Correct 40 ms 29096 KB Output is correct
36 Correct 40 ms 29008 KB Output is correct
37 Correct 42 ms 28912 KB Output is correct
38 Correct 39 ms 28928 KB Output is correct
39 Correct 39 ms 28976 KB Output is correct
40 Correct 68 ms 26968 KB Output is correct
41 Correct 68 ms 27000 KB Output is correct
42 Correct 72 ms 25932 KB Output is correct
43 Correct 70 ms 27012 KB Output is correct
44 Correct 52 ms 29052 KB Output is correct
45 Correct 56 ms 29116 KB Output is correct
46 Correct 59 ms 26984 KB Output is correct
47 Correct 55 ms 29032 KB Output is correct
48 Correct 265 ms 67288 KB Output is correct
49 Correct 269 ms 67336 KB Output is correct
50 Correct 266 ms 67336 KB Output is correct
51 Correct 259 ms 67260 KB Output is correct
52 Correct 249 ms 66320 KB Output is correct
53 Correct 588 ms 50128 KB Output is correct
54 Correct 669 ms 49176 KB Output is correct
55 Correct 677 ms 40624 KB Output is correct
56 Correct 714 ms 40364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23756 KB Output is correct
2 Correct 16 ms 23748 KB Output is correct
3 Correct 16 ms 23756 KB Output is correct
4 Correct 16 ms 23756 KB Output is correct
5 Correct 16 ms 23756 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 15 ms 23824 KB Output is correct
8 Correct 16 ms 23744 KB Output is correct
9 Correct 16 ms 23756 KB Output is correct
10 Correct 15 ms 23816 KB Output is correct
11 Correct 15 ms 23752 KB Output is correct
12 Correct 15 ms 23756 KB Output is correct
13 Correct 16 ms 23756 KB Output is correct
14 Correct 16 ms 23756 KB Output is correct
15 Correct 15 ms 23792 KB Output is correct
16 Correct 16 ms 23788 KB Output is correct
17 Correct 16 ms 23804 KB Output is correct
18 Correct 15 ms 23756 KB Output is correct
19 Correct 15 ms 23708 KB Output is correct
20 Correct 15 ms 23696 KB Output is correct
21 Correct 15 ms 23756 KB Output is correct
22 Correct 16 ms 23760 KB Output is correct
23 Correct 16 ms 23696 KB Output is correct
24 Correct 16 ms 23776 KB Output is correct
25 Correct 15 ms 23756 KB Output is correct
26 Correct 15 ms 23712 KB Output is correct
27 Correct 75 ms 25416 KB Output is correct
28 Correct 69 ms 25188 KB Output is correct
29 Correct 75 ms 25328 KB Output is correct
30 Correct 67 ms 25260 KB Output is correct
31 Correct 73 ms 25352 KB Output is correct
32 Correct 62 ms 25240 KB Output is correct
33 Correct 73 ms 25300 KB Output is correct
34 Correct 67 ms 25180 KB Output is correct
35 Correct 40 ms 29096 KB Output is correct
36 Correct 40 ms 29008 KB Output is correct
37 Correct 42 ms 28912 KB Output is correct
38 Correct 39 ms 28928 KB Output is correct
39 Correct 39 ms 28976 KB Output is correct
40 Correct 68 ms 26968 KB Output is correct
41 Correct 68 ms 27000 KB Output is correct
42 Correct 72 ms 25932 KB Output is correct
43 Correct 70 ms 27012 KB Output is correct
44 Correct 52 ms 29052 KB Output is correct
45 Correct 56 ms 29116 KB Output is correct
46 Correct 59 ms 26984 KB Output is correct
47 Correct 55 ms 29032 KB Output is correct
48 Correct 265 ms 67288 KB Output is correct
49 Correct 269 ms 67336 KB Output is correct
50 Correct 266 ms 67336 KB Output is correct
51 Correct 259 ms 67260 KB Output is correct
52 Correct 249 ms 66320 KB Output is correct
53 Correct 588 ms 50128 KB Output is correct
54 Correct 669 ms 49176 KB Output is correct
55 Correct 677 ms 40624 KB Output is correct
56 Correct 714 ms 40364 KB Output is correct
57 Correct 1301 ms 198184 KB Output is correct
58 Correct 1062 ms 123520 KB Output is correct
59 Correct 1052 ms 123440 KB Output is correct
60 Correct 1127 ms 127436 KB Output is correct
61 Correct 1201 ms 127432 KB Output is correct
62 Correct 901 ms 81876 KB Output is correct
63 Correct 977 ms 119060 KB Output is correct
64 Correct 932 ms 83768 KB Output is correct
65 Correct 831 ms 60160 KB Output is correct