Submission #637985

# Submission time Handle Problem Language Result Execution time Memory
637985 2022-09-04T01:45:15 Z Cross_Ratio Rope (JOI17_rope) C++14
100 / 100
1293 ms 88364 KB
#include <bits/stdc++.h>
using namespace std;
int A[1000005];
int B[1000005];
vector<vector<int>> adj;
int D[1000005];
int C[1000005];
int ans1[1000005];
int ans2[1000005];
map<int, int> M;
void push(int n) {
    M[n]++;
}
void pop(int n) {
    M[n]--;
    if(M[n]==0) M.erase(n);
}
int top() {
    if(M.size()==0) return 0;
    return M.rbegin()->first;
}
signed main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int N, Q;
    cin >> N >> Q;
    int i, j;
    for(i=0;i<N;i++) {
        cin >> A[i];
        A[i]--;
        B[A[i]]++;
    }
    adj.resize(N);
    for(i=0;i+1<N;i+=2) {
        adj[A[i]].push_back(A[i+1]);
        adj[A[i+1]].push_back(A[i]);
    }
    for(i=0;i<Q;i++) {
        push(B[i]);
    }
    for(i=0;i<Q;i++) {
        vector<int> V;
        for(int n : adj[i]) {
            if(n==i) continue;
            D[n]++;
            if(D[n]==1) V.push_back(n);
        }
        for(int n : V) {
            pop(B[n]);
            push(B[n]-D[n]);
        }
        pop(B[i]);
        int t = top();
        push(B[i]);
        for(int n : V) {
            pop(B[n]-D[n]);
            D[n] = 0;
            push(B[n]);
        }
        ans1[i] = N - t - B[i];
    }
    adj.clear();
    adj.resize(N);
    for(i=1;i+1<N;i+=2) {
        adj[A[i]].push_back(A[i+1]);
        adj[A[i+1]].push_back(A[i]);
    }
    for(i=0;i<Q;i++) {
        vector<int> V;
        for(int n : adj[i]) {
            if(n==i) continue;
            D[n]++;
            if(D[n]==1) V.push_back(n);
        }
        for(int n : V) {
            pop(B[n]);
            push(B[n]-D[n]);
        }
        pop(B[i]);
        int t = top();
        push(B[i]);
        for(int n : V) {
            pop(B[n]-D[n]);
            D[n] = 0;
            push(B[n]);
        }
        ans2[i] = N - t - B[i];
    }
    for(i=0;i<Q;i++) cout << min(ans1[i], ans2[i]) << '\n';
}

Compilation message

rope.cpp: In function 'int main()':
rope.cpp:28:12: warning: unused variable 'j' [-Wunused-variable]
   28 |     int i, j;
      |            ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 9 ms 3924 KB Output is correct
28 Correct 9 ms 3844 KB Output is correct
29 Correct 12 ms 3924 KB Output is correct
30 Correct 8 ms 3952 KB Output is correct
31 Correct 9 ms 3924 KB Output is correct
32 Correct 9 ms 3924 KB Output is correct
33 Correct 9 ms 3892 KB Output is correct
34 Correct 11 ms 4004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 9 ms 3924 KB Output is correct
28 Correct 9 ms 3844 KB Output is correct
29 Correct 12 ms 3924 KB Output is correct
30 Correct 8 ms 3952 KB Output is correct
31 Correct 9 ms 3924 KB Output is correct
32 Correct 9 ms 3924 KB Output is correct
33 Correct 9 ms 3892 KB Output is correct
34 Correct 11 ms 4004 KB Output is correct
35 Correct 38 ms 4060 KB Output is correct
36 Correct 37 ms 4068 KB Output is correct
37 Correct 37 ms 4052 KB Output is correct
38 Correct 35 ms 4028 KB Output is correct
39 Correct 36 ms 4072 KB Output is correct
40 Correct 20 ms 4180 KB Output is correct
41 Correct 22 ms 4180 KB Output is correct
42 Correct 20 ms 4120 KB Output is correct
43 Correct 19 ms 4052 KB Output is correct
44 Correct 21 ms 4144 KB Output is correct
45 Correct 24 ms 4176 KB Output is correct
46 Correct 18 ms 4028 KB Output is correct
47 Correct 19 ms 4060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 9 ms 3924 KB Output is correct
28 Correct 9 ms 3844 KB Output is correct
29 Correct 12 ms 3924 KB Output is correct
30 Correct 8 ms 3952 KB Output is correct
31 Correct 9 ms 3924 KB Output is correct
32 Correct 9 ms 3924 KB Output is correct
33 Correct 9 ms 3892 KB Output is correct
34 Correct 11 ms 4004 KB Output is correct
35 Correct 38 ms 4060 KB Output is correct
36 Correct 37 ms 4068 KB Output is correct
37 Correct 37 ms 4052 KB Output is correct
38 Correct 35 ms 4028 KB Output is correct
39 Correct 36 ms 4072 KB Output is correct
40 Correct 20 ms 4180 KB Output is correct
41 Correct 22 ms 4180 KB Output is correct
42 Correct 20 ms 4120 KB Output is correct
43 Correct 19 ms 4052 KB Output is correct
44 Correct 21 ms 4144 KB Output is correct
45 Correct 24 ms 4176 KB Output is correct
46 Correct 18 ms 4028 KB Output is correct
47 Correct 19 ms 4060 KB Output is correct
48 Correct 426 ms 39064 KB Output is correct
49 Correct 433 ms 39320 KB Output is correct
50 Correct 447 ms 39168 KB Output is correct
51 Correct 398 ms 38688 KB Output is correct
52 Correct 394 ms 37336 KB Output is correct
53 Correct 230 ms 38224 KB Output is correct
54 Correct 164 ms 37272 KB Output is correct
55 Correct 166 ms 37100 KB Output is correct
56 Correct 142 ms 36796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 9 ms 3924 KB Output is correct
28 Correct 9 ms 3844 KB Output is correct
29 Correct 12 ms 3924 KB Output is correct
30 Correct 8 ms 3952 KB Output is correct
31 Correct 9 ms 3924 KB Output is correct
32 Correct 9 ms 3924 KB Output is correct
33 Correct 9 ms 3892 KB Output is correct
34 Correct 11 ms 4004 KB Output is correct
35 Correct 38 ms 4060 KB Output is correct
36 Correct 37 ms 4068 KB Output is correct
37 Correct 37 ms 4052 KB Output is correct
38 Correct 35 ms 4028 KB Output is correct
39 Correct 36 ms 4072 KB Output is correct
40 Correct 20 ms 4180 KB Output is correct
41 Correct 22 ms 4180 KB Output is correct
42 Correct 20 ms 4120 KB Output is correct
43 Correct 19 ms 4052 KB Output is correct
44 Correct 21 ms 4144 KB Output is correct
45 Correct 24 ms 4176 KB Output is correct
46 Correct 18 ms 4028 KB Output is correct
47 Correct 19 ms 4060 KB Output is correct
48 Correct 426 ms 39064 KB Output is correct
49 Correct 433 ms 39320 KB Output is correct
50 Correct 447 ms 39168 KB Output is correct
51 Correct 398 ms 38688 KB Output is correct
52 Correct 394 ms 37336 KB Output is correct
53 Correct 230 ms 38224 KB Output is correct
54 Correct 164 ms 37272 KB Output is correct
55 Correct 166 ms 37100 KB Output is correct
56 Correct 142 ms 36796 KB Output is correct
57 Correct 1293 ms 88364 KB Output is correct
58 Correct 998 ms 66772 KB Output is correct
59 Correct 1051 ms 66804 KB Output is correct
60 Correct 1131 ms 72172 KB Output is correct
61 Correct 1090 ms 72180 KB Output is correct
62 Correct 427 ms 53468 KB Output is correct
63 Correct 638 ms 59636 KB Output is correct
64 Correct 588 ms 55800 KB Output is correct
65 Correct 257 ms 45976 KB Output is correct