# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
196946 | Akashi | Rope (JOI17_rope) | C++14 | 1193 ms | 92780 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;
const int N = 1e6 + 5;
vector <pair <int, int> > col;
vector <int> v[N];
int n, m;
int ans[N], f[N], x[N], cnt[N];
inline int calc(int x, int y){
return n - f[x] - f[y] + cnt[y];
}
void solve(int st, int dr){
for(int i = st; i < dr ; i += 2){
if(x[i] != x[i + 1]){
v[x[i]].push_back(x[i + 1]);
v[x[i + 1]].push_back(x[i]);
}
}
for(int i = 1; i <= m ; ++i){
for(auto it : v[i]) ++cnt[it];
for(auto it : v[i]) ans[i] = min(ans[i], calc(i, it));
int j = m - 1;
while(j >= 0 && (cnt[col[j].second] || col[j].second == i)) --j;
if(j >= 0) ans[i] = min(ans[i], calc(i, col[j].second));
for(auto it : v[i]) --cnt[it];
}
for(int i = 1; i <= m ; ++i) v[i].clear();
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= m ; ++i) ans[i] = n;
for(int i = 1; i <= n ; ++i){
scanf("%d", &x[i]);
++f[x[i]];
--ans[x[i]];
}
for(int i = 1; i <= m ; ++i) col.push_back({f[i], i});
sort(col.begin(), col.end());
solve(1, n - 1);
solve(2, n);
for(int i = 1; i <= m ; ++i)
printf("%d\n", ans[i]);
return 0;
}
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... |