Submission #1081224

#TimeUsernameProblemLanguageResultExecution timeMemory
1081224skittles1412Rope (JOI17_rope)C++17
100 / 100
266 ms66768 KiB
#include "bits/extc++.h" using namespace std; template <typename T, typename... U> void dbgh(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; } #ifdef DEBUG #define dbg(...) \ cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \ dbgh(__VA_ARGS__) #else #define dbg(...) #define cerr \ if (false) \ cerr #endif #define endl "\n" #define long int64_t #define sz(x) int(std::size(x)) inline void init_io() { cin.tie(nullptr); cin.exceptions(ios::failbit); ios_base::sync_with_stdio(false); } template <typename T> vector<T>& operator+=(vector<T>& a, const vector<T>& b) { a.insert(end(a), begin(b), end(b)); return a; } template <typename T> vector<T> operator+(vector<T> a, const vector<T>& b) { return a += b; } template <typename T> vector<T> iota(int n, const T& x) { vector<T> arr(n); iota(begin(arr), end(arr), x); return arr; } struct DS { vector<int> arr, occ; int cmx = 0; DS(int n) : arr(n), occ(int(1e6)) { for (int i = 0; i < n; i++) { occ[0]++; } } void update_add(int ind, int x) { occ[arr[ind]]--; occ[arr[ind] += x]++; cmx = max(cmx, arr[ind]); } void update_set(int ind, int x) { occ[arr[ind]]--; occ[arr[ind] = x]++; cmx = max(cmx, arr[ind]); } int query_max() { for (; !occ[cmx]; cmx--) ; return cmx; } int operator[](int ind) const { return arr[ind]; } }; vector<int> solve(int m, const vector<int>& arr) { int n = sz(arr); assert(n % 2 == 0); DS cnto(m); vector<int> cnt2(m); vector<basic_string<int>> graph(m); for (int i = 0; i < n; i += 2) { int a = arr[i], b = arr[i + 1]; cnto.update_add(a, +1); cnto.update_add(b, +1); if (a == b) { cnt2[a]++; continue; } graph[a].push_back(b); graph[b].push_back(a); } vector<int> ans(m); for (int c = 0; c < m; c++) { int cx = 0; for (auto& v : graph[c]) { cnto.update_add(v, -1); } int tmpr = cnto[c]; cnto.update_set(c, 0); cx += 2 * cnt2[c]; cx += sz(graph[c]); cx += cnto.query_max(); ans[c] = n - cx; for (auto& v : graph[c]) { cnto.update_add(v, +1); } cnto.update_set(c, tmpr); } return ans; } void solve() { int n, m; cin >> n >> m; vector<int> arr(n); for (auto& a : arr) { cin >> a; a--; } vector<int> ans(m, 1e9); auto go = [&](const vector<int>& narr, int dd) -> void { auto cans = solve(m + dd, narr); for (int i = 0; i < m; i++) { ans[i] = min(ans[i], cans[i] - dd); } }; if (n % 2 == 0) { go(arr, 0); go(vector {m} + arr + vector {m + 1}, 2); } else { go(arr + vector {m}, 1); go(vector {m} + arr, 1); } for (auto& a : ans) { cout << a << endl; } } int main() { init_io(); solve(); }
#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...