제출 #490591

#제출 시각아이디문제언어결과실행 시간메모리
490591fhvirusRope (JOI17_rope)C++17
100 / 100
1290 ms84484 KiB
// Knapsack DP is harder than FFT. #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define ff first #define ss second #define pb emplace_back #define AI(x) begin(x),end(x) template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;} template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;} #ifdef OWO #define debug(args...) SDF(#args, args) #define OIU(args...) ostream& operator<<(ostream&O,args) #define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;} LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss) template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));} template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';} #else #pragma GCC optimize("Ofast") #define debug(...) ((void)0) #endif signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int N, M; cin >> N >> M; vector<int> rope(N); for (int &i: rope) cin >> i; vector<int> cnt(M+1, 0); vector<int> num(N+1, 0); int mxv = 0; num[0] = M; auto add = [&](int v) { --num[cnt[v]]; ++cnt[v]; ++num[cnt[v]]; if(cnt[v] > mxv) mxv = cnt[v]; }; auto rem = [&](int v) { --num[cnt[v]]; --cnt[v]; ++num[cnt[v]]; if(num[mxv] == 0) --mxv; }; for (int &i: rope) add(i); vector<int> ans(M+1, N); for (int k = 0; k < 2; ++k) { vector<vector<int>> adj(M+1, vector<int>()); if(k == 1) adj[rope[0]].pb(0); for (int i = k; i < N; i += 2) { int u = rope[i]; int v = (i + 1 < N ? rope[i + 1] : 0); adj[u].pb(v); if (u != v and v != 0) adj[v].pb(u); } for (int i = 1; i <= M; ++i) { int cur = 0; for (int &j: adj[i]) { ++cur; rem(i); if (j == i) ++cur; if (j != 0) rem(j); } ans[i] = min(ans[i], N - cur - mxv); for (int &j: adj[i]) { add(i); if (j != 0) add(j); } } } for (int i = 1; i <= M; ++i) cout << ans[i] << '\n'; return 0; }
#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...