Submission #519645

# Submission time Handle Problem Language Result Execution time Memory
519645 2022-01-26T22:19:10 Z Giantpizzahead Rope (JOI17_rope) C++17
100 / 100
1309 ms 58392 KB
/*
Solution: 
Runtime: 
*/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define sz(x) ((int) x.size())
#define all(x) x.begin(), x.end()
#define debug if (true) cerr
using ll = long long;

const int MAXN = 1e6+5;
const int INF = 1e9;

int N, M;
int C[MAXN], saved[MAXN], ans[MAXN];
vector<pair<int, int>> toTry;
map<pair<int, int>, int> conflicts;

void genPairs(bool isOdd) {
    // Clear previous data
    rep(i, 0, M) saved[i] = 0;
    toTry.clear();
    conflicts.clear();
    // Generate pairs
    for (int i = isOdd; i < N; i += 2) {
        if (i == N-1) {
            // Final one
            saved[C[i]]++;
        } else {
            // Pair
            int j = i+1;
            if (C[i] == C[j]) saved[C[i]] += 2;
            else {
                saved[C[i]]++;
                saved[C[j]]++;
                pair<int, int> p = {min(C[i], C[j]), max(C[i], C[j])};
                conflicts[p]++;
            }
        }
    }
    if (isOdd) saved[C[0]]++;
}

void updateAns() {
    // Sort best colors to pair with
    rep(i, 0, M) toTry.push_back({saved[i], i});
    sort(all(toTry));
    reverse(all(toTry));
    // Update answer for each
    rep(a, 0, M) {
        // Change all to one color
        ans[a] = min(N - saved[a], ans[a]);
        for (auto& p : toTry) {
            int b = p.second;
            if (a == b) continue;
            auto ptr = conflicts.find({min(a, b), max(a, b)});
            if (ptr == conflicts.end()) {
                // This is the best pairing; can stop here
                ans[a] = min(N - saved[a] - saved[b], ans[a]);
                break;
            } else {
                // Process manually and continue searching
                ans[a] = min(N - saved[a] - saved[b] + ptr->second, ans[a]);
            }
        }
    }
}

void solve() {
    cin >> N >> M;
    rep(i, 0, N) {
        cin >> C[i];
        C[i]--;
    }
    rep(i, 0, M) ans[i] = INF;

    // Pairs start at even indexes
    genPairs(false);
    updateAns();
    // Pairs start at odd indexes
    genPairs(true);
    updateAns();
    
    rep(i, 0, M) cout << ans[i] << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 216 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 216 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 9 ms 716 KB Output is correct
28 Correct 6 ms 716 KB Output is correct
29 Correct 11 ms 736 KB Output is correct
30 Correct 9 ms 664 KB Output is correct
31 Correct 9 ms 728 KB Output is correct
32 Correct 7 ms 716 KB Output is correct
33 Correct 8 ms 716 KB Output is correct
34 Correct 7 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 216 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 9 ms 716 KB Output is correct
28 Correct 6 ms 716 KB Output is correct
29 Correct 11 ms 736 KB Output is correct
30 Correct 9 ms 664 KB Output is correct
31 Correct 9 ms 728 KB Output is correct
32 Correct 7 ms 716 KB Output is correct
33 Correct 8 ms 716 KB Output is correct
34 Correct 7 ms 716 KB Output is correct
35 Correct 35 ms 3276 KB Output is correct
36 Correct 33 ms 3288 KB Output is correct
37 Correct 48 ms 3184 KB Output is correct
38 Correct 34 ms 3272 KB Output is correct
39 Correct 33 ms 3240 KB Output is correct
40 Correct 23 ms 1996 KB Output is correct
41 Correct 24 ms 1972 KB Output is correct
42 Correct 22 ms 1776 KB Output is correct
43 Correct 30 ms 1740 KB Output is correct
44 Correct 24 ms 2056 KB Output is correct
45 Correct 24 ms 2036 KB Output is correct
46 Correct 26 ms 1836 KB Output is correct
47 Correct 22 ms 1764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 216 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 9 ms 716 KB Output is correct
28 Correct 6 ms 716 KB Output is correct
29 Correct 11 ms 736 KB Output is correct
30 Correct 9 ms 664 KB Output is correct
31 Correct 9 ms 728 KB Output is correct
32 Correct 7 ms 716 KB Output is correct
33 Correct 8 ms 716 KB Output is correct
34 Correct 7 ms 716 KB Output is correct
35 Correct 35 ms 3276 KB Output is correct
36 Correct 33 ms 3288 KB Output is correct
37 Correct 48 ms 3184 KB Output is correct
38 Correct 34 ms 3272 KB Output is correct
39 Correct 33 ms 3240 KB Output is correct
40 Correct 23 ms 1996 KB Output is correct
41 Correct 24 ms 1972 KB Output is correct
42 Correct 22 ms 1776 KB Output is correct
43 Correct 30 ms 1740 KB Output is correct
44 Correct 24 ms 2056 KB Output is correct
45 Correct 24 ms 2036 KB Output is correct
46 Correct 26 ms 1836 KB Output is correct
47 Correct 22 ms 1764 KB Output is correct
48 Correct 997 ms 33928 KB Output is correct
49 Correct 969 ms 33956 KB Output is correct
50 Correct 981 ms 34116 KB Output is correct
51 Correct 880 ms 29536 KB Output is correct
52 Correct 739 ms 24020 KB Output is correct
53 Correct 528 ms 20140 KB Output is correct
54 Correct 401 ms 15916 KB Output is correct
55 Correct 388 ms 15580 KB Output is correct
56 Correct 416 ms 13736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 216 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 9 ms 716 KB Output is correct
28 Correct 6 ms 716 KB Output is correct
29 Correct 11 ms 736 KB Output is correct
30 Correct 9 ms 664 KB Output is correct
31 Correct 9 ms 728 KB Output is correct
32 Correct 7 ms 716 KB Output is correct
33 Correct 8 ms 716 KB Output is correct
34 Correct 7 ms 716 KB Output is correct
35 Correct 35 ms 3276 KB Output is correct
36 Correct 33 ms 3288 KB Output is correct
37 Correct 48 ms 3184 KB Output is correct
38 Correct 34 ms 3272 KB Output is correct
39 Correct 33 ms 3240 KB Output is correct
40 Correct 23 ms 1996 KB Output is correct
41 Correct 24 ms 1972 KB Output is correct
42 Correct 22 ms 1776 KB Output is correct
43 Correct 30 ms 1740 KB Output is correct
44 Correct 24 ms 2056 KB Output is correct
45 Correct 24 ms 2036 KB Output is correct
46 Correct 26 ms 1836 KB Output is correct
47 Correct 22 ms 1764 KB Output is correct
48 Correct 997 ms 33928 KB Output is correct
49 Correct 969 ms 33956 KB Output is correct
50 Correct 981 ms 34116 KB Output is correct
51 Correct 880 ms 29536 KB Output is correct
52 Correct 739 ms 24020 KB Output is correct
53 Correct 528 ms 20140 KB Output is correct
54 Correct 401 ms 15916 KB Output is correct
55 Correct 388 ms 15580 KB Output is correct
56 Correct 416 ms 13736 KB Output is correct
57 Correct 1295 ms 58392 KB Output is correct
58 Correct 1309 ms 49188 KB Output is correct
59 Correct 1265 ms 49244 KB Output is correct
60 Correct 1281 ms 51436 KB Output is correct
61 Correct 1273 ms 51568 KB Output is correct
62 Correct 695 ms 27168 KB Output is correct
63 Correct 886 ms 35664 KB Output is correct
64 Correct 752 ms 31260 KB Output is correct
65 Correct 600 ms 19900 KB Output is correct