Submission #519646

# Submission time Handle Problem Language Result Execution time Memory
519646 2022-01-26T22:28:01 Z Giantpizzahead Rope (JOI17_rope) C++17
100 / 100
1286 ms 58344 KB
/*
Solution: The problem statement can be simplified as follows.
Given an array C of N integers, each in the range [1, M]. (Guaranteed at least 1 of each color)
For each c in [1, M], what's the minimum # of elements you must change to create an array that:
- Only has one or two colors, one of which is c
- Can be separated into contiguous ranges of same elements, with the middle ranges having an even size

After fixing 2 colors, how to find minimum cost quickly?
. . 1 . 2 2 . 1 2 . . . 2 1 1 . 2 . . . 1
Fix parity (location of evens)
. . 1 . 2 2 . 1 2 . . . 2 1 1 . 2 . . . 1
   ^   ^   ^   ^   ^   ^   ^   ^   ^   ^
For each pair, optimal cost can be easily determined
No match (. .) = 2
One match (1 ., . 1, 2 ., . 2) = 1
Both match (1 2, 2 1) = 1
Same match (1 1, 2 2) = 0
Might need to add one element matching either of the target colors on the ends (special for loc 0 and N-1)
Don't need to fix two colors to calc above cost (can process each independently), it doesn't affect anything
O(NM^2)

Total cost of a pair = sum of min(cost 1, cost 2) for each element
Each pair can only have up to one color = 0, only up to two colors = 1, everything else will be = 2
Total cost of a pair = 2 * (# of pairs) - Places with improvement

Only 1s will conflict, and only conflict between 2 colors (call these "conflict pairs")
Total cost of a pair =
2 * (# of pairs) - 2 * # with 0 in either color - # with 1 in either color + # where colors conflict
O(N+M^2)

To get full credit, evaluate conflict pairs manually, and just take color that saves the most for all other colors
- Simply iterate to next one if conflict arises (happens at most N times)

Runtime: O(N+M * log(M))
*/

#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 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 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 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 204 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 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 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 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 204 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 724 KB Output is correct
29 Correct 8 ms 612 KB Output is correct
30 Correct 7 ms 716 KB Output is correct
31 Correct 8 ms 716 KB Output is correct
32 Correct 7 ms 724 KB Output is correct
33 Correct 8 ms 716 KB Output is correct
34 Correct 7 ms 728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 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 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 204 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 724 KB Output is correct
29 Correct 8 ms 612 KB Output is correct
30 Correct 7 ms 716 KB Output is correct
31 Correct 8 ms 716 KB Output is correct
32 Correct 7 ms 724 KB Output is correct
33 Correct 8 ms 716 KB Output is correct
34 Correct 7 ms 728 KB Output is correct
35 Correct 33 ms 3276 KB Output is correct
36 Correct 35 ms 3424 KB Output is correct
37 Correct 35 ms 3256 KB Output is correct
38 Correct 33 ms 3276 KB Output is correct
39 Correct 38 ms 3192 KB Output is correct
40 Correct 26 ms 1996 KB Output is correct
41 Correct 25 ms 1996 KB Output is correct
42 Correct 25 ms 1788 KB Output is correct
43 Correct 24 ms 1740 KB Output is correct
44 Correct 24 ms 2132 KB Output is correct
45 Correct 23 ms 2108 KB Output is correct
46 Correct 22 ms 1740 KB Output is correct
47 Correct 23 ms 1712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 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 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 204 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 724 KB Output is correct
29 Correct 8 ms 612 KB Output is correct
30 Correct 7 ms 716 KB Output is correct
31 Correct 8 ms 716 KB Output is correct
32 Correct 7 ms 724 KB Output is correct
33 Correct 8 ms 716 KB Output is correct
34 Correct 7 ms 728 KB Output is correct
35 Correct 33 ms 3276 KB Output is correct
36 Correct 35 ms 3424 KB Output is correct
37 Correct 35 ms 3256 KB Output is correct
38 Correct 33 ms 3276 KB Output is correct
39 Correct 38 ms 3192 KB Output is correct
40 Correct 26 ms 1996 KB Output is correct
41 Correct 25 ms 1996 KB Output is correct
42 Correct 25 ms 1788 KB Output is correct
43 Correct 24 ms 1740 KB Output is correct
44 Correct 24 ms 2132 KB Output is correct
45 Correct 23 ms 2108 KB Output is correct
46 Correct 22 ms 1740 KB Output is correct
47 Correct 23 ms 1712 KB Output is correct
48 Correct 955 ms 34024 KB Output is correct
49 Correct 982 ms 33996 KB Output is correct
50 Correct 964 ms 33984 KB Output is correct
51 Correct 890 ms 29524 KB Output is correct
52 Correct 720 ms 24020 KB Output is correct
53 Correct 496 ms 20108 KB Output is correct
54 Correct 392 ms 15904 KB Output is correct
55 Correct 382 ms 15560 KB Output is correct
56 Correct 355 ms 13748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 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 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 204 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 724 KB Output is correct
29 Correct 8 ms 612 KB Output is correct
30 Correct 7 ms 716 KB Output is correct
31 Correct 8 ms 716 KB Output is correct
32 Correct 7 ms 724 KB Output is correct
33 Correct 8 ms 716 KB Output is correct
34 Correct 7 ms 728 KB Output is correct
35 Correct 33 ms 3276 KB Output is correct
36 Correct 35 ms 3424 KB Output is correct
37 Correct 35 ms 3256 KB Output is correct
38 Correct 33 ms 3276 KB Output is correct
39 Correct 38 ms 3192 KB Output is correct
40 Correct 26 ms 1996 KB Output is correct
41 Correct 25 ms 1996 KB Output is correct
42 Correct 25 ms 1788 KB Output is correct
43 Correct 24 ms 1740 KB Output is correct
44 Correct 24 ms 2132 KB Output is correct
45 Correct 23 ms 2108 KB Output is correct
46 Correct 22 ms 1740 KB Output is correct
47 Correct 23 ms 1712 KB Output is correct
48 Correct 955 ms 34024 KB Output is correct
49 Correct 982 ms 33996 KB Output is correct
50 Correct 964 ms 33984 KB Output is correct
51 Correct 890 ms 29524 KB Output is correct
52 Correct 720 ms 24020 KB Output is correct
53 Correct 496 ms 20108 KB Output is correct
54 Correct 392 ms 15904 KB Output is correct
55 Correct 382 ms 15560 KB Output is correct
56 Correct 355 ms 13748 KB Output is correct
57 Correct 1266 ms 58344 KB Output is correct
58 Correct 1286 ms 49344 KB Output is correct
59 Correct 1193 ms 49168 KB Output is correct
60 Correct 1245 ms 51488 KB Output is correct
61 Correct 1229 ms 51712 KB Output is correct
62 Correct 662 ms 27168 KB Output is correct
63 Correct 840 ms 35756 KB Output is correct
64 Correct 746 ms 31224 KB Output is correct
65 Correct 464 ms 19928 KB Output is correct