답안 #758153

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
758153 2023-06-14T08:22:24 Z PixelCat Rope (JOI17_rope) C++14
55 / 100
2500 ms 127584 KB
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
#define int LL
#define INF (int)(1e18)
using namespace std;
using LL = long long;
using pii = pair<int, int>;

void chmin(int &x, int a) {
    x = min(x, a);
}
void chmax(int &x, int a) {
    x = max(x, a);
}

const int MAXN = 1000010;

int ans[MAXN];
int cnt1[MAXN];

struct Ayaya {
    int a[MAXN];
    multiset<int> st;
    void init(int n) {
        memset(a, 0, sizeof(a));
        st.clear();
        For(i, 1, n) st.insert(0);
    }
    void upd(int i, int x) {
        st.erase(st.find(a[i]));
        a[i] = x;
        st.insert(a[i]);
    }
    void add(int i, int x) {
        upd(i, a[i] + x);
    }
    int min() {
        return *st.begin();
    }
} aya;

void solve(int n, int m, vector<pii> v2, vector<int> v1) {
    memset(cnt1, 0, sizeof(cnt1));
    map<pii, int> cnt2;
    for(auto &i:v2) {
        cnt1[i.F]++;
        cnt1[i.S]++;
        cnt2[i]++;
        cnt2[pii(i.S, i.F)]++;
    }
    vector<vector<pii>> vv(m + 1, vector<pii>());
    for(auto &i:cnt2) {
        vv[i.F.F].eb(i.F.S, i.S);
    }
    aya.init(m);
    For(c2, 1, m) aya.upd(c2, n - cnt1[c2]);
    // 2, 2
    // 2 1, 1 3, 3 2, 1 1
    // 2 2, 1 1, 3 3, 2 1, 1 2
    For(c1, 1, m) {
        int add = 0;
        for(auto &p:vv[c1]) {
            aya.add(p.F, p.S);
        }
        for(auto &i:v1) {
            aya.add(i, -1);
            if(i == c1) add--;
        }
        aya.add(c1, INF);
        chmin(ans[c1], aya.min() - cnt1[c1] + add);
        // cout << aya.min() << ", " << add << ", " << (aya.min() - cnt1[c1] + add) << ": ";
        // For(c2, 1, m) cout << (aya.a[c2] >= INF ? -1 : aya.a[c2]) << " \n"[c2 == m];
        aya.add(c1, -INF);
        for(auto &i:v1) aya.add(i, 1);
        for(auto &p:vv[c1]) aya.add(p.F, -p.S);
        // For(c2, c1 + 1, m) {
        //     int cost = n - cnt1[c1] - cnt1[c2];
        //     if(cnt2.count(pii(c1, c2))) cost += cnt2[pii(c1, c2)];
        //     for(auto &i:v1) {
        //         cost -= max(i == c1, i == c2);
        //     }
        //     chmin(ans[c1], cost);
        //     chmin(ans[c2], cost);
        // }
        int cost = n - cnt1[c1];
        for(auto &i:v1) cost -= (i == c1);
        chmin(ans[c1], cost);
    }
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // =^-w-^=
    int n, m; cin >> n >> m;
    vector<int> a(n);
    for(auto &i:a) cin >> i;

    For(i, 1, m) {
        ans[i] = INF;
    }

    if(n % 2) {
        For(_, 0, 1) {
            vector<pii> v2;
            for(int i = 1; i < n; i += 2) {
                v2.eb(pii(a[i - 1], a[i]));
            }
            solve(n, m, v2, { a.back() });
            reverse(all(a));
        }
    } else {
        vector<pii> v2;
        for(int i = 1; i < n; i += 2) {
            v2.eb(pii(a[i - 1], a[i]));
        }
        solve(n, m, v2, {});

        v2.clear();
        for(int i = 2; i < n; i += 2) {
            v2.eb(pii(a[i - 1], a[i]));
        }
        solve(n, m, v2, { a.front(), a.back() });
    }

    For(i, 1, m) cout << ans[i] << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 15988 KB Output is correct
2 Correct 13 ms 15952 KB Output is correct
3 Correct 10 ms 15956 KB Output is correct
4 Correct 10 ms 15956 KB Output is correct
5 Correct 10 ms 15864 KB Output is correct
6 Correct 10 ms 15992 KB Output is correct
7 Correct 9 ms 15956 KB Output is correct
8 Correct 9 ms 15956 KB Output is correct
9 Correct 10 ms 15960 KB Output is correct
10 Correct 10 ms 15916 KB Output is correct
11 Correct 9 ms 15952 KB Output is correct
12 Correct 9 ms 15992 KB Output is correct
13 Correct 9 ms 15956 KB Output is correct
14 Correct 9 ms 15992 KB Output is correct
15 Correct 9 ms 15956 KB Output is correct
16 Correct 9 ms 15960 KB Output is correct
17 Correct 9 ms 15952 KB Output is correct
18 Correct 11 ms 15948 KB Output is correct
19 Correct 10 ms 15924 KB Output is correct
20 Correct 10 ms 15956 KB Output is correct
21 Correct 9 ms 15956 KB Output is correct
22 Correct 9 ms 15956 KB Output is correct
23 Correct 9 ms 15956 KB Output is correct
24 Correct 11 ms 15876 KB Output is correct
25 Correct 10 ms 15960 KB Output is correct
26 Correct 9 ms 15880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 15988 KB Output is correct
2 Correct 13 ms 15952 KB Output is correct
3 Correct 10 ms 15956 KB Output is correct
4 Correct 10 ms 15956 KB Output is correct
5 Correct 10 ms 15864 KB Output is correct
6 Correct 10 ms 15992 KB Output is correct
7 Correct 9 ms 15956 KB Output is correct
8 Correct 9 ms 15956 KB Output is correct
9 Correct 10 ms 15960 KB Output is correct
10 Correct 10 ms 15916 KB Output is correct
11 Correct 9 ms 15952 KB Output is correct
12 Correct 9 ms 15992 KB Output is correct
13 Correct 9 ms 15956 KB Output is correct
14 Correct 9 ms 15992 KB Output is correct
15 Correct 9 ms 15956 KB Output is correct
16 Correct 9 ms 15960 KB Output is correct
17 Correct 9 ms 15952 KB Output is correct
18 Correct 11 ms 15948 KB Output is correct
19 Correct 10 ms 15924 KB Output is correct
20 Correct 10 ms 15956 KB Output is correct
21 Correct 9 ms 15956 KB Output is correct
22 Correct 9 ms 15956 KB Output is correct
23 Correct 9 ms 15956 KB Output is correct
24 Correct 11 ms 15876 KB Output is correct
25 Correct 10 ms 15960 KB Output is correct
26 Correct 9 ms 15880 KB Output is correct
27 Correct 24 ms 18632 KB Output is correct
28 Correct 19 ms 18572 KB Output is correct
29 Correct 28 ms 18764 KB Output is correct
30 Correct 27 ms 18728 KB Output is correct
31 Correct 25 ms 18584 KB Output is correct
32 Correct 20 ms 18640 KB Output is correct
33 Correct 23 ms 18768 KB Output is correct
34 Correct 21 ms 18768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 15988 KB Output is correct
2 Correct 13 ms 15952 KB Output is correct
3 Correct 10 ms 15956 KB Output is correct
4 Correct 10 ms 15956 KB Output is correct
5 Correct 10 ms 15864 KB Output is correct
6 Correct 10 ms 15992 KB Output is correct
7 Correct 9 ms 15956 KB Output is correct
8 Correct 9 ms 15956 KB Output is correct
9 Correct 10 ms 15960 KB Output is correct
10 Correct 10 ms 15916 KB Output is correct
11 Correct 9 ms 15952 KB Output is correct
12 Correct 9 ms 15992 KB Output is correct
13 Correct 9 ms 15956 KB Output is correct
14 Correct 9 ms 15992 KB Output is correct
15 Correct 9 ms 15956 KB Output is correct
16 Correct 9 ms 15960 KB Output is correct
17 Correct 9 ms 15952 KB Output is correct
18 Correct 11 ms 15948 KB Output is correct
19 Correct 10 ms 15924 KB Output is correct
20 Correct 10 ms 15956 KB Output is correct
21 Correct 9 ms 15956 KB Output is correct
22 Correct 9 ms 15956 KB Output is correct
23 Correct 9 ms 15956 KB Output is correct
24 Correct 11 ms 15876 KB Output is correct
25 Correct 10 ms 15960 KB Output is correct
26 Correct 9 ms 15880 KB Output is correct
27 Correct 24 ms 18632 KB Output is correct
28 Correct 19 ms 18572 KB Output is correct
29 Correct 28 ms 18764 KB Output is correct
30 Correct 27 ms 18728 KB Output is correct
31 Correct 25 ms 18584 KB Output is correct
32 Correct 20 ms 18640 KB Output is correct
33 Correct 23 ms 18768 KB Output is correct
34 Correct 21 ms 18768 KB Output is correct
35 Correct 193 ms 25920 KB Output is correct
36 Correct 157 ms 25956 KB Output is correct
37 Correct 151 ms 25976 KB Output is correct
38 Correct 169 ms 26104 KB Output is correct
39 Correct 179 ms 26144 KB Output is correct
40 Correct 85 ms 22476 KB Output is correct
41 Correct 79 ms 22336 KB Output is correct
42 Correct 72 ms 21680 KB Output is correct
43 Correct 72 ms 21336 KB Output is correct
44 Correct 83 ms 22536 KB Output is correct
45 Correct 86 ms 22488 KB Output is correct
46 Correct 71 ms 21688 KB Output is correct
47 Correct 82 ms 21296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 15988 KB Output is correct
2 Correct 13 ms 15952 KB Output is correct
3 Correct 10 ms 15956 KB Output is correct
4 Correct 10 ms 15956 KB Output is correct
5 Correct 10 ms 15864 KB Output is correct
6 Correct 10 ms 15992 KB Output is correct
7 Correct 9 ms 15956 KB Output is correct
8 Correct 9 ms 15956 KB Output is correct
9 Correct 10 ms 15960 KB Output is correct
10 Correct 10 ms 15916 KB Output is correct
11 Correct 9 ms 15952 KB Output is correct
12 Correct 9 ms 15992 KB Output is correct
13 Correct 9 ms 15956 KB Output is correct
14 Correct 9 ms 15992 KB Output is correct
15 Correct 9 ms 15956 KB Output is correct
16 Correct 9 ms 15960 KB Output is correct
17 Correct 9 ms 15952 KB Output is correct
18 Correct 11 ms 15948 KB Output is correct
19 Correct 10 ms 15924 KB Output is correct
20 Correct 10 ms 15956 KB Output is correct
21 Correct 9 ms 15956 KB Output is correct
22 Correct 9 ms 15956 KB Output is correct
23 Correct 9 ms 15956 KB Output is correct
24 Correct 11 ms 15876 KB Output is correct
25 Correct 10 ms 15960 KB Output is correct
26 Correct 9 ms 15880 KB Output is correct
27 Correct 24 ms 18632 KB Output is correct
28 Correct 19 ms 18572 KB Output is correct
29 Correct 28 ms 18764 KB Output is correct
30 Correct 27 ms 18728 KB Output is correct
31 Correct 25 ms 18584 KB Output is correct
32 Correct 20 ms 18640 KB Output is correct
33 Correct 23 ms 18768 KB Output is correct
34 Correct 21 ms 18768 KB Output is correct
35 Correct 193 ms 25920 KB Output is correct
36 Correct 157 ms 25956 KB Output is correct
37 Correct 151 ms 25976 KB Output is correct
38 Correct 169 ms 26104 KB Output is correct
39 Correct 179 ms 26144 KB Output is correct
40 Correct 85 ms 22476 KB Output is correct
41 Correct 79 ms 22336 KB Output is correct
42 Correct 72 ms 21680 KB Output is correct
43 Correct 72 ms 21336 KB Output is correct
44 Correct 83 ms 22536 KB Output is correct
45 Correct 86 ms 22488 KB Output is correct
46 Correct 71 ms 21688 KB Output is correct
47 Correct 82 ms 21296 KB Output is correct
48 Execution timed out 2555 ms 127584 KB Time limit exceeded
49 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 15988 KB Output is correct
2 Correct 13 ms 15952 KB Output is correct
3 Correct 10 ms 15956 KB Output is correct
4 Correct 10 ms 15956 KB Output is correct
5 Correct 10 ms 15864 KB Output is correct
6 Correct 10 ms 15992 KB Output is correct
7 Correct 9 ms 15956 KB Output is correct
8 Correct 9 ms 15956 KB Output is correct
9 Correct 10 ms 15960 KB Output is correct
10 Correct 10 ms 15916 KB Output is correct
11 Correct 9 ms 15952 KB Output is correct
12 Correct 9 ms 15992 KB Output is correct
13 Correct 9 ms 15956 KB Output is correct
14 Correct 9 ms 15992 KB Output is correct
15 Correct 9 ms 15956 KB Output is correct
16 Correct 9 ms 15960 KB Output is correct
17 Correct 9 ms 15952 KB Output is correct
18 Correct 11 ms 15948 KB Output is correct
19 Correct 10 ms 15924 KB Output is correct
20 Correct 10 ms 15956 KB Output is correct
21 Correct 9 ms 15956 KB Output is correct
22 Correct 9 ms 15956 KB Output is correct
23 Correct 9 ms 15956 KB Output is correct
24 Correct 11 ms 15876 KB Output is correct
25 Correct 10 ms 15960 KB Output is correct
26 Correct 9 ms 15880 KB Output is correct
27 Correct 24 ms 18632 KB Output is correct
28 Correct 19 ms 18572 KB Output is correct
29 Correct 28 ms 18764 KB Output is correct
30 Correct 27 ms 18728 KB Output is correct
31 Correct 25 ms 18584 KB Output is correct
32 Correct 20 ms 18640 KB Output is correct
33 Correct 23 ms 18768 KB Output is correct
34 Correct 21 ms 18768 KB Output is correct
35 Correct 193 ms 25920 KB Output is correct
36 Correct 157 ms 25956 KB Output is correct
37 Correct 151 ms 25976 KB Output is correct
38 Correct 169 ms 26104 KB Output is correct
39 Correct 179 ms 26144 KB Output is correct
40 Correct 85 ms 22476 KB Output is correct
41 Correct 79 ms 22336 KB Output is correct
42 Correct 72 ms 21680 KB Output is correct
43 Correct 72 ms 21336 KB Output is correct
44 Correct 83 ms 22536 KB Output is correct
45 Correct 86 ms 22488 KB Output is correct
46 Correct 71 ms 21688 KB Output is correct
47 Correct 82 ms 21296 KB Output is correct
48 Execution timed out 2555 ms 127584 KB Time limit exceeded
49 Halted 0 ms 0 KB -