Submission #1081216

# Submission time Handle Problem Language Result Execution time Memory
1081216 2024-08-29T19:48:08 Z skittles1412 Rope (JOI17_rope) C++17
80 / 100
2500 ms 143920 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;
}

struct DS {
    vector<int> arr;
    multiset<int> ms;

    DS(int n) : arr(n) {
        for (int i = 0; i < n; i++) {
            ms.insert(0);
        }
    }

    void update_add(int ind, int x) {
        ms.erase(ms.find(arr[ind]));
        ms.insert(arr[ind] += x);
    }

    void update_set(int ind, int x) {
        ms.erase(ms.find(arr[ind]));
        ms.insert(arr[ind] = x);
    }

    int query_max() const {
        return *rbegin(ms);
    }

    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<vector<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();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 456 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 456 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 37 ms 2768 KB Output is correct
28 Correct 22 ms 2848 KB Output is correct
29 Correct 52 ms 2256 KB Output is correct
30 Correct 24 ms 2256 KB Output is correct
31 Correct 34 ms 2812 KB Output is correct
32 Correct 27 ms 2616 KB Output is correct
33 Correct 35 ms 2360 KB Output is correct
34 Correct 24 ms 2056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 456 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 37 ms 2768 KB Output is correct
28 Correct 22 ms 2848 KB Output is correct
29 Correct 52 ms 2256 KB Output is correct
30 Correct 24 ms 2256 KB Output is correct
31 Correct 34 ms 2812 KB Output is correct
32 Correct 27 ms 2616 KB Output is correct
33 Correct 35 ms 2360 KB Output is correct
34 Correct 24 ms 2056 KB Output is correct
35 Correct 79 ms 3032 KB Output is correct
36 Correct 77 ms 3016 KB Output is correct
37 Correct 82 ms 2640 KB Output is correct
38 Correct 77 ms 2516 KB Output is correct
39 Correct 78 ms 2644 KB Output is correct
40 Correct 61 ms 3084 KB Output is correct
41 Correct 62 ms 3068 KB Output is correct
42 Correct 54 ms 3064 KB Output is correct
43 Correct 58 ms 3056 KB Output is correct
44 Correct 70 ms 3012 KB Output is correct
45 Correct 63 ms 3084 KB Output is correct
46 Correct 59 ms 3000 KB Output is correct
47 Correct 62 ms 3056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 456 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 37 ms 2768 KB Output is correct
28 Correct 22 ms 2848 KB Output is correct
29 Correct 52 ms 2256 KB Output is correct
30 Correct 24 ms 2256 KB Output is correct
31 Correct 34 ms 2812 KB Output is correct
32 Correct 27 ms 2616 KB Output is correct
33 Correct 35 ms 2360 KB Output is correct
34 Correct 24 ms 2056 KB Output is correct
35 Correct 79 ms 3032 KB Output is correct
36 Correct 77 ms 3016 KB Output is correct
37 Correct 82 ms 2640 KB Output is correct
38 Correct 77 ms 2516 KB Output is correct
39 Correct 78 ms 2644 KB Output is correct
40 Correct 61 ms 3084 KB Output is correct
41 Correct 62 ms 3068 KB Output is correct
42 Correct 54 ms 3064 KB Output is correct
43 Correct 58 ms 3056 KB Output is correct
44 Correct 70 ms 3012 KB Output is correct
45 Correct 63 ms 3084 KB Output is correct
46 Correct 59 ms 3000 KB Output is correct
47 Correct 62 ms 3056 KB Output is correct
48 Correct 1070 ms 27664 KB Output is correct
49 Correct 1070 ms 27692 KB Output is correct
50 Correct 981 ms 23732 KB Output is correct
51 Correct 881 ms 23052 KB Output is correct
52 Correct 846 ms 21768 KB Output is correct
53 Correct 725 ms 26792 KB Output is correct
54 Correct 651 ms 25936 KB Output is correct
55 Correct 641 ms 25612 KB Output is correct
56 Correct 616 ms 25356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 456 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 37 ms 2768 KB Output is correct
28 Correct 22 ms 2848 KB Output is correct
29 Correct 52 ms 2256 KB Output is correct
30 Correct 24 ms 2256 KB Output is correct
31 Correct 34 ms 2812 KB Output is correct
32 Correct 27 ms 2616 KB Output is correct
33 Correct 35 ms 2360 KB Output is correct
34 Correct 24 ms 2056 KB Output is correct
35 Correct 79 ms 3032 KB Output is correct
36 Correct 77 ms 3016 KB Output is correct
37 Correct 82 ms 2640 KB Output is correct
38 Correct 77 ms 2516 KB Output is correct
39 Correct 78 ms 2644 KB Output is correct
40 Correct 61 ms 3084 KB Output is correct
41 Correct 62 ms 3068 KB Output is correct
42 Correct 54 ms 3064 KB Output is correct
43 Correct 58 ms 3056 KB Output is correct
44 Correct 70 ms 3012 KB Output is correct
45 Correct 63 ms 3084 KB Output is correct
46 Correct 59 ms 3000 KB Output is correct
47 Correct 62 ms 3056 KB Output is correct
48 Correct 1070 ms 27664 KB Output is correct
49 Correct 1070 ms 27692 KB Output is correct
50 Correct 981 ms 23732 KB Output is correct
51 Correct 881 ms 23052 KB Output is correct
52 Correct 846 ms 21768 KB Output is correct
53 Correct 725 ms 26792 KB Output is correct
54 Correct 651 ms 25936 KB Output is correct
55 Correct 641 ms 25612 KB Output is correct
56 Correct 616 ms 25356 KB Output is correct
57 Execution timed out 2564 ms 143920 KB Time limit exceeded
58 Halted 0 ms 0 KB -