답안 #1081224

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1081224 2024-08-29T19:53:41 Z skittles1412 Rope (JOI17_rope) C++17
100 / 100
266 ms 66768 KB
#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

inline void init_io() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
}

template <typename T>
vector<T>& operator+=(vector<T>& a, const vector<T>& b) {
    a.insert(end(a), begin(b), end(b));
    return a;
}

template <typename T>
vector<T> operator+(vector<T> a, const vector<T>& b) {
    return a += b;
}

template <typename T>
vector<T> iota(int n, const T& x) {
    vector<T> arr(n);
    iota(begin(arr), end(arr), x);
    return arr;
}

struct DS {
    vector<int> arr, occ;
    int cmx = 0;

    DS(int n) : arr(n), occ(int(1e6)) {
        for (int i = 0; i < n; i++) {
            occ[0]++;
        }
    }

    void update_add(int ind, int x) {
        occ[arr[ind]]--;
        occ[arr[ind] += x]++;
        cmx = max(cmx, arr[ind]);
    }

    void update_set(int ind, int x) {
        occ[arr[ind]]--;
        occ[arr[ind] = x]++;
        cmx = max(cmx, arr[ind]);
    }

    int query_max() {
        for (; !occ[cmx]; cmx--)
            ;
        return cmx;
    }

    int operator[](int ind) const {
        return arr[ind];
    }
};

vector<int> solve(int m, const vector<int>& arr) {
    int n = sz(arr);
    assert(n % 2 == 0);

    DS cnto(m);
    vector<int> cnt2(m);
    vector<basic_string<int>> graph(m);

    for (int i = 0; i < n; i += 2) {
        int a = arr[i], b = arr[i + 1];

        cnto.update_add(a, +1);
        cnto.update_add(b, +1);

        if (a == b) {
            cnt2[a]++;
            continue;
        }

        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    vector<int> ans(m);

    for (int c = 0; c < m; c++) {
        int cx = 0;

        for (auto& v : graph[c]) {
            cnto.update_add(v, -1);
        }
        int tmpr = cnto[c];
        cnto.update_set(c, 0);

        cx += 2 * cnt2[c];
        cx += sz(graph[c]);
        cx += cnto.query_max();
        ans[c] = n - cx;

        for (auto& v : graph[c]) {
            cnto.update_add(v, +1);
        }
        cnto.update_set(c, tmpr);
    }

    return ans;
}

void solve() {
    int n, m;
    cin >> n >> m;

    vector<int> arr(n);
    for (auto& a : arr) {
        cin >> a;
        a--;
    }

    vector<int> ans(m, 1e9);

    auto go = [&](const vector<int>& narr, int dd) -> void {
        auto cans = solve(m + dd, narr);
        for (int i = 0; i < m; i++) {
            ans[i] = min(ans[i], cans[i] - dd);
        }
    };

    if (n % 2 == 0) {
        go(arr, 0);
        go(vector {m} + arr + vector {m + 1}, 2);
    } else {
        go(arr + vector {m}, 1);
        go(vector {m} + arr, 1);
    }

    for (auto& a : ans) {
        cout << a << endl;
    }
}

int main() {
    init_io();
    solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4372 KB Output is correct
2 Correct 3 ms 4376 KB Output is correct
3 Correct 3 ms 4376 KB Output is correct
4 Correct 3 ms 4376 KB Output is correct
5 Correct 3 ms 4376 KB Output is correct
6 Correct 3 ms 4376 KB Output is correct
7 Correct 3 ms 4376 KB Output is correct
8 Correct 3 ms 4304 KB Output is correct
9 Correct 3 ms 4372 KB Output is correct
10 Correct 3 ms 4372 KB Output is correct
11 Correct 3 ms 4376 KB Output is correct
12 Correct 3 ms 4376 KB Output is correct
13 Correct 3 ms 4376 KB Output is correct
14 Correct 3 ms 4376 KB Output is correct
15 Correct 3 ms 4372 KB Output is correct
16 Correct 3 ms 4376 KB Output is correct
17 Correct 3 ms 4376 KB Output is correct
18 Correct 3 ms 4296 KB Output is correct
19 Correct 3 ms 4376 KB Output is correct
20 Correct 3 ms 4260 KB Output is correct
21 Correct 3 ms 4376 KB Output is correct
22 Correct 3 ms 4376 KB Output is correct
23 Correct 3 ms 4372 KB Output is correct
24 Correct 3 ms 4372 KB Output is correct
25 Correct 3 ms 4376 KB Output is correct
26 Correct 3 ms 4376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4372 KB Output is correct
2 Correct 3 ms 4376 KB Output is correct
3 Correct 3 ms 4376 KB Output is correct
4 Correct 3 ms 4376 KB Output is correct
5 Correct 3 ms 4376 KB Output is correct
6 Correct 3 ms 4376 KB Output is correct
7 Correct 3 ms 4376 KB Output is correct
8 Correct 3 ms 4304 KB Output is correct
9 Correct 3 ms 4372 KB Output is correct
10 Correct 3 ms 4372 KB Output is correct
11 Correct 3 ms 4376 KB Output is correct
12 Correct 3 ms 4376 KB Output is correct
13 Correct 3 ms 4376 KB Output is correct
14 Correct 3 ms 4376 KB Output is correct
15 Correct 3 ms 4372 KB Output is correct
16 Correct 3 ms 4376 KB Output is correct
17 Correct 3 ms 4376 KB Output is correct
18 Correct 3 ms 4296 KB Output is correct
19 Correct 3 ms 4376 KB Output is correct
20 Correct 3 ms 4260 KB Output is correct
21 Correct 3 ms 4376 KB Output is correct
22 Correct 3 ms 4376 KB Output is correct
23 Correct 3 ms 4372 KB Output is correct
24 Correct 3 ms 4372 KB Output is correct
25 Correct 3 ms 4376 KB Output is correct
26 Correct 3 ms 4376 KB Output is correct
27 Correct 11 ms 6300 KB Output is correct
28 Correct 10 ms 6256 KB Output is correct
29 Correct 10 ms 5904 KB Output is correct
30 Correct 10 ms 6108 KB Output is correct
31 Correct 10 ms 6324 KB Output is correct
32 Correct 11 ms 6308 KB Output is correct
33 Correct 10 ms 5892 KB Output is correct
34 Correct 10 ms 5896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4372 KB Output is correct
2 Correct 3 ms 4376 KB Output is correct
3 Correct 3 ms 4376 KB Output is correct
4 Correct 3 ms 4376 KB Output is correct
5 Correct 3 ms 4376 KB Output is correct
6 Correct 3 ms 4376 KB Output is correct
7 Correct 3 ms 4376 KB Output is correct
8 Correct 3 ms 4304 KB Output is correct
9 Correct 3 ms 4372 KB Output is correct
10 Correct 3 ms 4372 KB Output is correct
11 Correct 3 ms 4376 KB Output is correct
12 Correct 3 ms 4376 KB Output is correct
13 Correct 3 ms 4376 KB Output is correct
14 Correct 3 ms 4376 KB Output is correct
15 Correct 3 ms 4372 KB Output is correct
16 Correct 3 ms 4376 KB Output is correct
17 Correct 3 ms 4376 KB Output is correct
18 Correct 3 ms 4296 KB Output is correct
19 Correct 3 ms 4376 KB Output is correct
20 Correct 3 ms 4260 KB Output is correct
21 Correct 3 ms 4376 KB Output is correct
22 Correct 3 ms 4376 KB Output is correct
23 Correct 3 ms 4372 KB Output is correct
24 Correct 3 ms 4372 KB Output is correct
25 Correct 3 ms 4376 KB Output is correct
26 Correct 3 ms 4376 KB Output is correct
27 Correct 11 ms 6300 KB Output is correct
28 Correct 10 ms 6256 KB Output is correct
29 Correct 10 ms 5904 KB Output is correct
30 Correct 10 ms 6108 KB Output is correct
31 Correct 10 ms 6324 KB Output is correct
32 Correct 11 ms 6308 KB Output is correct
33 Correct 10 ms 5892 KB Output is correct
34 Correct 10 ms 5896 KB Output is correct
35 Correct 11 ms 6680 KB Output is correct
36 Correct 11 ms 6680 KB Output is correct
37 Correct 14 ms 6356 KB Output is correct
38 Correct 11 ms 6352 KB Output is correct
39 Correct 10 ms 6152 KB Output is correct
40 Correct 10 ms 6424 KB Output is correct
41 Correct 10 ms 6548 KB Output is correct
42 Correct 10 ms 6420 KB Output is correct
43 Correct 9 ms 6424 KB Output is correct
44 Correct 10 ms 6612 KB Output is correct
45 Correct 10 ms 6420 KB Output is correct
46 Correct 10 ms 6428 KB Output is correct
47 Correct 10 ms 6260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4372 KB Output is correct
2 Correct 3 ms 4376 KB Output is correct
3 Correct 3 ms 4376 KB Output is correct
4 Correct 3 ms 4376 KB Output is correct
5 Correct 3 ms 4376 KB Output is correct
6 Correct 3 ms 4376 KB Output is correct
7 Correct 3 ms 4376 KB Output is correct
8 Correct 3 ms 4304 KB Output is correct
9 Correct 3 ms 4372 KB Output is correct
10 Correct 3 ms 4372 KB Output is correct
11 Correct 3 ms 4376 KB Output is correct
12 Correct 3 ms 4376 KB Output is correct
13 Correct 3 ms 4376 KB Output is correct
14 Correct 3 ms 4376 KB Output is correct
15 Correct 3 ms 4372 KB Output is correct
16 Correct 3 ms 4376 KB Output is correct
17 Correct 3 ms 4376 KB Output is correct
18 Correct 3 ms 4296 KB Output is correct
19 Correct 3 ms 4376 KB Output is correct
20 Correct 3 ms 4260 KB Output is correct
21 Correct 3 ms 4376 KB Output is correct
22 Correct 3 ms 4376 KB Output is correct
23 Correct 3 ms 4372 KB Output is correct
24 Correct 3 ms 4372 KB Output is correct
25 Correct 3 ms 4376 KB Output is correct
26 Correct 3 ms 4376 KB Output is correct
27 Correct 11 ms 6300 KB Output is correct
28 Correct 10 ms 6256 KB Output is correct
29 Correct 10 ms 5904 KB Output is correct
30 Correct 10 ms 6108 KB Output is correct
31 Correct 10 ms 6324 KB Output is correct
32 Correct 11 ms 6308 KB Output is correct
33 Correct 10 ms 5892 KB Output is correct
34 Correct 10 ms 5896 KB Output is correct
35 Correct 11 ms 6680 KB Output is correct
36 Correct 11 ms 6680 KB Output is correct
37 Correct 14 ms 6356 KB Output is correct
38 Correct 11 ms 6352 KB Output is correct
39 Correct 10 ms 6152 KB Output is correct
40 Correct 10 ms 6424 KB Output is correct
41 Correct 10 ms 6548 KB Output is correct
42 Correct 10 ms 6420 KB Output is correct
43 Correct 9 ms 6424 KB Output is correct
44 Correct 10 ms 6612 KB Output is correct
45 Correct 10 ms 6420 KB Output is correct
46 Correct 10 ms 6428 KB Output is correct
47 Correct 10 ms 6260 KB Output is correct
48 Correct 82 ms 25024 KB Output is correct
49 Correct 82 ms 25104 KB Output is correct
50 Correct 88 ms 21184 KB Output is correct
51 Correct 76 ms 21136 KB Output is correct
52 Correct 98 ms 22800 KB Output is correct
53 Correct 79 ms 27148 KB Output is correct
54 Correct 80 ms 26832 KB Output is correct
55 Correct 107 ms 26820 KB Output is correct
56 Correct 77 ms 26560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4372 KB Output is correct
2 Correct 3 ms 4376 KB Output is correct
3 Correct 3 ms 4376 KB Output is correct
4 Correct 3 ms 4376 KB Output is correct
5 Correct 3 ms 4376 KB Output is correct
6 Correct 3 ms 4376 KB Output is correct
7 Correct 3 ms 4376 KB Output is correct
8 Correct 3 ms 4304 KB Output is correct
9 Correct 3 ms 4372 KB Output is correct
10 Correct 3 ms 4372 KB Output is correct
11 Correct 3 ms 4376 KB Output is correct
12 Correct 3 ms 4376 KB Output is correct
13 Correct 3 ms 4376 KB Output is correct
14 Correct 3 ms 4376 KB Output is correct
15 Correct 3 ms 4372 KB Output is correct
16 Correct 3 ms 4376 KB Output is correct
17 Correct 3 ms 4376 KB Output is correct
18 Correct 3 ms 4296 KB Output is correct
19 Correct 3 ms 4376 KB Output is correct
20 Correct 3 ms 4260 KB Output is correct
21 Correct 3 ms 4376 KB Output is correct
22 Correct 3 ms 4376 KB Output is correct
23 Correct 3 ms 4372 KB Output is correct
24 Correct 3 ms 4372 KB Output is correct
25 Correct 3 ms 4376 KB Output is correct
26 Correct 3 ms 4376 KB Output is correct
27 Correct 11 ms 6300 KB Output is correct
28 Correct 10 ms 6256 KB Output is correct
29 Correct 10 ms 5904 KB Output is correct
30 Correct 10 ms 6108 KB Output is correct
31 Correct 10 ms 6324 KB Output is correct
32 Correct 11 ms 6308 KB Output is correct
33 Correct 10 ms 5892 KB Output is correct
34 Correct 10 ms 5896 KB Output is correct
35 Correct 11 ms 6680 KB Output is correct
36 Correct 11 ms 6680 KB Output is correct
37 Correct 14 ms 6356 KB Output is correct
38 Correct 11 ms 6352 KB Output is correct
39 Correct 10 ms 6152 KB Output is correct
40 Correct 10 ms 6424 KB Output is correct
41 Correct 10 ms 6548 KB Output is correct
42 Correct 10 ms 6420 KB Output is correct
43 Correct 9 ms 6424 KB Output is correct
44 Correct 10 ms 6612 KB Output is correct
45 Correct 10 ms 6420 KB Output is correct
46 Correct 10 ms 6428 KB Output is correct
47 Correct 10 ms 6260 KB Output is correct
48 Correct 82 ms 25024 KB Output is correct
49 Correct 82 ms 25104 KB Output is correct
50 Correct 88 ms 21184 KB Output is correct
51 Correct 76 ms 21136 KB Output is correct
52 Correct 98 ms 22800 KB Output is correct
53 Correct 79 ms 27148 KB Output is correct
54 Correct 80 ms 26832 KB Output is correct
55 Correct 107 ms 26820 KB Output is correct
56 Correct 77 ms 26560 KB Output is correct
57 Correct 266 ms 66768 KB Output is correct
58 Correct 207 ms 55576 KB Output is correct
59 Correct 198 ms 55660 KB Output is correct
60 Correct 230 ms 60020 KB Output is correct
61 Correct 210 ms 60084 KB Output is correct
62 Correct 137 ms 45188 KB Output is correct
63 Correct 153 ms 51520 KB Output is correct
64 Correct 144 ms 47568 KB Output is correct
65 Correct 115 ms 38772 KB Output is correct