제출 #1312148

#제출 시각아이디문제언어결과실행 시간메모리
1312148steveonalexRLE (BOI06_rle)C++20
100 / 100
1179 ms129272 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ul; typedef unsigned long long ull; #define MASK(i) (1ULL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll gcd(ll a, ll b){return __gcd(a, b);} ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ull mask){return __builtin_ctzll(mask);} int logOf(ull mask){return 63 - __builtin_clzll(mask);} // mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rng(1); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T the_array_itself, string separator = " ", string finish = "\n", ostream &out = cout){ for(auto item: the_array_itself) out << item << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const ll INF = 1e18 + 69; struct SegmentTree{ int n; vector<pair<ll, ll>> a; SegmentTree(int _n){ n = _n; a.resize(n * 2 + 2, make_pair(-INF, -INF)); for(int i = 1; i <= n; ++i) a[i + n] = make_pair(-INF, i); for(int i = n; i >= 1; --i) a[i] = max(a[i * 2], a[i * 2 + 1]); } void update(int i, ll v){ i += n; a[i].first = v; while(i > 1){ i >>= 1; a[i] = max(a[i * 2], a[i * 2 + 1]); } } pair<ll, ll> get(int l, int r){ l += n; r += n + 1; pair<ll, ll> ans = make_pair(-INF, -INF); while(l < r){ if (l & 1) maximize(ans, a[l++]); if (r & 1) maximize(ans, a[--r]); l >>= 1; r >>= 1; } return ans; } }; void solve(){ int n; cin >> n; int m; cin >> m; vector<int> a(m); for(int i = 0; i < m; ++i) cin >> a[i]; vector<pair<ll,ll>> b; int e = 0; for(int i = 0; i < m; ){ if (a[i] != e){ if (b.empty() || b.back().first != a[i]) b.push_back({a[i], 1}); else b.back().second++; ++i; } else{ if (a[i+1] == e){ int k = a[i+2]; if (b.empty() || b.back().first != e) b.push_back({e, k+1}); else b.back().second += k+1; } else{ if (a[i+2] == 0){ e = a[i+1]; } else{ int k = a[i+2]; if (b.empty() || b.back().first != a[i+1]) b.push_back({a[i+1], k+3}); else b.back().second += k+3; } } i += 3; } } SegmentTree st(n), rst(n); st.update(1, 0); rst.update(1, 0); for(int i = 2; i <= n; ++i) { st.update(i, 3); rst.update(i, -3); } ll tot = 0; vector<vector<pair<int,int>>> awesome(n+1); for(int i = 2; i <= n; ++i) awesome[i].push_back({0, 1}); int t = 0; for(pair<ll, ll> i: b){ i.first++; t++; ll cost1 = 0, cost2 = 0; ll tmp = i.second; cost1 = (tmp / (n+2)) * 3; tmp %= n+2; cost1 += min(tmp, 3); tmp = i.second; cost2 = (tmp / n) * 3; tmp %= n; if (tmp) cost2 += 3; tot += cost1; pair<ll, ll> val = st.get(i.first, i.first); st.update(i.first, val.first + cost2 - cost1); rst.update(i.first, -(val.first + cost2 - cost1)); pair<ll, ll> mi = rst.get(1, n); mi.first *= -1; while(true){ pair<ll, ll> ma = st.get(1, n); if (ma.first > mi.first + 3){ st.update(ma.second, mi.first + 3); rst.update(ma.second, -(mi.first+3)); awesome[ma.second].push_back({t, mi.second}); } else break; } } pair<ll, ll> mi = rst.get(1, n); mi.first *= -1; cout << mi.first + tot << "\n"; t = b.size(); e = mi.second; vector<int> color(t+1, -1); while(t > 0){ int idx = lower_bound(ALL(awesome[e]), make_pair(t, 0)) - awesome[e].begin(); idx--; if (idx < 0) break; pair<ll, ll> cur = awesome[e][idx]; t = cur.first; color[t] = e; e = cur.second; } e = 1; vector<ll> ans; for(int i = 0; i < (int) b.size(); ++i){ if (color[i] != -1){ ans.push_back(e-1); ans.push_back(color[i]-1); ans.push_back(0); e = color[i]; } pair<ll, ll> it = b[i]; it.first++; if (it.first == e){ while(it.second >= n){ ans.push_back(e-1); ans.push_back(e-1); ans.push_back(n-1); it.second -= n; } if (it.second){ ans.push_back(e-1); ans.push_back(e-1); ans.push_back(it.second-1); } } else{ while(it.second >= n + 2){ ans.push_back(e-1); ans.push_back(it.first-1); ans.push_back(n-1); it.second -= n+2; } if (it.second <= 3){ while(it.second--) ans.push_back(it.first-1); } else{ ans.push_back(e-1); ans.push_back(it.first-1); ans.push_back(it.second-3); } } } printArr(ans); } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t start = clock(); solve(); cerr << "Time elapsed: " << clock() - start << "ms!\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...