Submission #637985

#TimeUsernameProblemLanguageResultExecution timeMemory
637985Cross_RatioRope (JOI17_rope)C++14
100 / 100
1293 ms88364 KiB
#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 (stderr)

rope.cpp: In function 'int main()':
rope.cpp:28:12: warning: unused variable 'j' [-Wunused-variable]
   28 |     int i, j;
      |            ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...