답안 #519644

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
519644 2022-01-26T22:15:46 Z Giantpizzahead Rope (JOI17_rope) C++17
100 / 100
1355 ms 65196 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 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
    for (int i = 0; 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]++;
            }
        }
    }
    // 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]);
            }
        }
    }

    // Pairs start at odd indexes
    rep(i, 0, M) saved[i] = 0;
    toTry.clear();
    conflicts.clear();
    for (int i = 1; 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]++;
            }
        }
    }
    saved[C[0]]++;
    // 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]);
            }
        }
    }
    // debug << "toTry\n";
    // for (auto& p : toTry) debug << p.first << " " << p.second << endl;

    rep(i, 0, M) cout << ans[i] << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 320 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 320 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 836 KB Output is correct
28 Correct 8 ms 908 KB Output is correct
29 Correct 10 ms 972 KB Output is correct
30 Correct 7 ms 836 KB Output is correct
31 Correct 10 ms 940 KB Output is correct
32 Correct 8 ms 844 KB Output is correct
33 Correct 9 ms 848 KB Output is correct
34 Correct 7 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 320 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 836 KB Output is correct
28 Correct 8 ms 908 KB Output is correct
29 Correct 10 ms 972 KB Output is correct
30 Correct 7 ms 836 KB Output is correct
31 Correct 10 ms 940 KB Output is correct
32 Correct 8 ms 844 KB Output is correct
33 Correct 9 ms 848 KB Output is correct
34 Correct 7 ms 856 KB Output is correct
35 Correct 36 ms 3592 KB Output is correct
36 Correct 35 ms 3556 KB Output is correct
37 Correct 33 ms 3552 KB Output is correct
38 Correct 34 ms 3628 KB Output is correct
39 Correct 37 ms 3552 KB Output is correct
40 Correct 24 ms 2340 KB Output is correct
41 Correct 24 ms 2436 KB Output is correct
42 Correct 28 ms 2160 KB Output is correct
43 Correct 29 ms 2048 KB Output is correct
44 Correct 25 ms 2484 KB Output is correct
45 Correct 24 ms 2484 KB Output is correct
46 Correct 23 ms 2128 KB Output is correct
47 Correct 22 ms 2124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 320 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 836 KB Output is correct
28 Correct 8 ms 908 KB Output is correct
29 Correct 10 ms 972 KB Output is correct
30 Correct 7 ms 836 KB Output is correct
31 Correct 10 ms 940 KB Output is correct
32 Correct 8 ms 844 KB Output is correct
33 Correct 9 ms 848 KB Output is correct
34 Correct 7 ms 856 KB Output is correct
35 Correct 36 ms 3592 KB Output is correct
36 Correct 35 ms 3556 KB Output is correct
37 Correct 33 ms 3552 KB Output is correct
38 Correct 34 ms 3628 KB Output is correct
39 Correct 37 ms 3552 KB Output is correct
40 Correct 24 ms 2340 KB Output is correct
41 Correct 24 ms 2436 KB Output is correct
42 Correct 28 ms 2160 KB Output is correct
43 Correct 29 ms 2048 KB Output is correct
44 Correct 25 ms 2484 KB Output is correct
45 Correct 24 ms 2484 KB Output is correct
46 Correct 23 ms 2128 KB Output is correct
47 Correct 22 ms 2124 KB Output is correct
48 Correct 1004 ms 38368 KB Output is correct
49 Correct 1014 ms 38460 KB Output is correct
50 Correct 1002 ms 38404 KB Output is correct
51 Correct 927 ms 33700 KB Output is correct
52 Correct 763 ms 27800 KB Output is correct
53 Correct 506 ms 24668 KB Output is correct
54 Correct 425 ms 20448 KB Output is correct
55 Correct 430 ms 20064 KB Output is correct
56 Correct 379 ms 18176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 320 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 836 KB Output is correct
28 Correct 8 ms 908 KB Output is correct
29 Correct 10 ms 972 KB Output is correct
30 Correct 7 ms 836 KB Output is correct
31 Correct 10 ms 940 KB Output is correct
32 Correct 8 ms 844 KB Output is correct
33 Correct 9 ms 848 KB Output is correct
34 Correct 7 ms 856 KB Output is correct
35 Correct 36 ms 3592 KB Output is correct
36 Correct 35 ms 3556 KB Output is correct
37 Correct 33 ms 3552 KB Output is correct
38 Correct 34 ms 3628 KB Output is correct
39 Correct 37 ms 3552 KB Output is correct
40 Correct 24 ms 2340 KB Output is correct
41 Correct 24 ms 2436 KB Output is correct
42 Correct 28 ms 2160 KB Output is correct
43 Correct 29 ms 2048 KB Output is correct
44 Correct 25 ms 2484 KB Output is correct
45 Correct 24 ms 2484 KB Output is correct
46 Correct 23 ms 2128 KB Output is correct
47 Correct 22 ms 2124 KB Output is correct
48 Correct 1004 ms 38368 KB Output is correct
49 Correct 1014 ms 38460 KB Output is correct
50 Correct 1002 ms 38404 KB Output is correct
51 Correct 927 ms 33700 KB Output is correct
52 Correct 763 ms 27800 KB Output is correct
53 Correct 506 ms 24668 KB Output is correct
54 Correct 425 ms 20448 KB Output is correct
55 Correct 430 ms 20064 KB Output is correct
56 Correct 379 ms 18176 KB Output is correct
57 Correct 1355 ms 65196 KB Output is correct
58 Correct 1270 ms 55900 KB Output is correct
59 Correct 1257 ms 55872 KB Output is correct
60 Correct 1284 ms 58180 KB Output is correct
61 Correct 1289 ms 58208 KB Output is correct
62 Correct 616 ms 33584 KB Output is correct
63 Correct 866 ms 42324 KB Output is correct
64 Correct 773 ms 37776 KB Output is correct
65 Correct 477 ms 26112 KB Output is correct