# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
637985 | Cross_Ratio | Rope (JOI17_rope) | C++14 | 1293 ms | 88364 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |