Submission #196946

#TimeUsernameProblemLanguageResultExecution timeMemory
196946AkashiRope (JOI17_rope)C++14
100 / 100
1193 ms92780 KiB
#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)

rope.cpp: In function 'int main()':
rope.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
rope.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &x[i]);
         ~~~~~^~~~~~~~~~~~~
#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...