Submission #169555

#TimeUsernameProblemLanguageResultExecution timeMemory
169555combi1k1Rope (JOI17_rope)C++14
100 / 100
944 ms80724 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define sz(x) int(x.size()) const int N = 1e6 + 5; int a[N], c[N]; vector<int> pos[N]; vector<int> idx; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int m; cin >> m; a[n] = m; for(int i = 0 ; i < n ; ++i) { int x; cin >> x; --x; a[i] = x; c[x]--; pos[x].pb(i); } idx.resize(m); iota(idx.begin(),idx.end(),0); sort(idx.begin(),idx.end(),[&](int x,int y) { return c[x] < c[y]; }); for(int i = 0 ; i < m ; ++i) { int ans = n - sz(pos[i]); for(int p = 0 ; p < 2 ; ++p) { int cnt = 2; int add = n; for(int x : pos[i]) { if (x > 0 && x % 2 != p) c[a[x - 1]]++, cnt++; if (x < n && x % 2 == p) c[a[x + 1]]++, cnt++; } if (cnt > m) cnt = m; for(int j = 0 ; j < cnt ; ++j) if (add > c[idx[j]] && idx[j] != i) add = c[idx[j]]; if (ans > n - sz(pos[i]) + add) ans = n - sz(pos[i]) + add; for(int x : pos[i]) { if (x > 0 && x % 2 != p) c[a[x - 1]]--; if (x < n && x % 2 == p) c[a[x + 1]]--; } } cout << ans << "\n"; } }
#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...