#include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; /*#include <atcoder/modint> using namespace atcoder; using mint = modint998244353; vector <mint> fac, inv; mint binom(int n, int k) { if (n < k || n < 0) return 0; return fac[n] * inv[k] * inv[n-k]; } void prep(int N) { fac.resize(N+1, 1); inv.resize(N+1, 1); for (int i=1; i<=N; ++i) fac[i] = fac[i-1] * i; inv[N] = fac[N].inv(); for (int i=N-1; i>0; --i) inv[i] = inv[i+1] * (i + 1); }*/ mt19937_64 rng(2137); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif const int maxn = 1000100; //Did you REAALLY consider sample tests? ll zlodzieje(vector <int> vec) { ll opt = 0; ll minpref = 0; ll pref = 0; for (int i=0; i<sz(vec); ++i) { pref += vec[i]; opt = max(opt, pref - minpref); minpref = min(minpref, pref); } return opt; } ll better(vector <int> vec) { ll opt = 0; ll curr = 0; trav(elem, vec) { curr += elem; opt = max(opt, curr); if (curr < 0) curr = 0; } return opt; } const ll INF = LLONG_MAX / 4; ll brut(vi A, vi B, int k) { int n = sz(A); int all = (1 << n); ll ukradna = INF; rep(mask, 0, all) { if (__builtin_popcount(mask) != k) continue; vi muzeum(n); rep(i, 0, n) { if (mask & (1 << i)) muzeum[i] = A[i]; else muzeum[i] = B[i]; } ukradna = min(ukradna, zlodzieje(muzeum)); assert(zlodzieje(muzeum) == better(muzeum)); } return ukradna; } /*ll solve(vector <ll> costs, vi init, int ile_wziac) { int n = sz(costs); //debug(init); while (ile_wziac > 0) { --ile_wziac; int best = -1; ll ukradna = INF; rep(i, 0, n) { if (costs[i] == INF) continue; init[i] += costs[i]; ll moze_byc = zlodzieje(init); init[i] -= costs[i]; if (moze_byc < ukradna) { ukradna = moze_byc; best = i; } } assert(best != -1); init[best] += costs[best]; costs[best] = INF; //debug(init); } return zlodzieje(init); }*/ struct stan { ll maks; ll psum; bool alice; stan* previous_state; stan(ll ma, ll ps, bool ali, stan*previous) : maks(ma), psum(ps), alice(ali), previous_state(previous) {} }; bool cmp(stan*ten, stan*other) { if (ten->maks != other->maks) return ten->maks < other->maks; if (ten->psum != other->psum) return ten->psum < other->psum; return ten->previous_state < other->previous_state; } vector <stan*> remove_suboptimal(vector <stan*> states) { if (states.empty()) return states; sort(all(states), cmp); // better score, smaller suffix -> better state overall vector <stan*> zostaja = {states[0]}; for (int i=1; i<sz(states); ++i) { if (zostaja.back()->psum <= states[i]->psum) continue; zostaja.push_back(states[i]); } return zostaja; } //a.insert(a.end(), b.begin(), b.end()); vector <stan*> add_element(vector <stan*> states, ll nowy, bool alice) { vector <stan*> result(sz(states), nullptr); for (int i=0; i<sz(states); ++i) { ll maks = states[i]->maks, psum = states[i]->psum; psum += nowy; if (psum < 0) psum = 0; maks = max(maks, psum); result[i] = new stan(maks, psum, alice, states[i]); } return result; } const int MAGIC = 10; ll slow_state_solution(vi A, vi B, int k) { int n = sz(A); vi inputA(n), inputB(n); rep(i, 0, n) inputA[i] = A[i], inputB[i] = B[i]; bool lifehack = false; if (k >= n - k) { lifehack = true; swap(A, B); k = n - k; } vector <vector<stan*>> best(k + 1); best[0].pb(new stan(0, 0, 0, nullptr)); rep(i, 0, n) { vector<vector<stan*>> nowy(k+1); FOR(j, 0, k) { int remains = n - i; if (j + remains < k) continue; auto alice = add_element(best[j], A[i], true); auto bob = add_element(best[j], B[i], false); if (j < k) nowy[j + 1].insert(nowy[j + 1].end(), all(alice)); nowy[j].insert(nowy[j].end(), all(bob)); } FOR(j, 0, k) best[j] = remove_suboptimal(nowy[j]); nowy.clear(); nowy.shrink_to_fit(); FOR(j, 0, k) { //bad state trimming if (sz(best[j]) >= MAGIC) best[j].resize(MAGIC); } } auto verify = [&](string s) -> ll { vector <int> vec(n); rep(i, 0, n) { if (s[i] == 'A') vec[i] = inputA[i]; else vec[i] = inputB[i]; } return better(vec); }; stan* ostatni = best[k][0]; ll wynik = ostatni -> maks; string odp = ""; FOR(steps, 1, n) { if (ostatni->alice) odp += 'A'; else odp += 'B'; ostatni = ostatni->previous_state; } reverse(all(odp)); if (lifehack) { rep(i, 0, n) { if (odp[i] == 'A') odp[i] = 'B'; else odp[i] = 'A'; } } //PRZEMYSL ASSERTY /*if (verify(odp) != wynik) { OUT("UPS"); }*/ cout << wynik << "\n"; cout << odp << "\n"; return wynik; } ll wzo(vi A, vi B, int k) { int n = sz(A); vll init(n); vll costs(n); rep(i, 0, n) init[i] = min(A[i], B[i]); int alice = 0; rep(i, 0, n) { if (A[i] <= B[i]) ++alice, init[i] = A[i]; else init[i] = B[i]; } if (alice < k) { //chcemy wiecej obrazow A rep(i, 0, n) { if (A[i] > B[i]) costs[i] = A[i] - B[i]; else costs[i] = INF; } } else { //chcemy wiecej obrazow B rep(i, 0, n) { if (A[i] <= B[i]) costs[i] = B[i] - A[i]; else costs[i] = INF; } } return 0; //return heura(costs, init, abs(k - alice)); } bool TEST = 0; int main() { boost; if (!TEST) { int n, k; cin >> n >> k; vi A(n), B(n); rep(i, 0, n) cin >> A[i]; rep(i, 0, n) cin >> B[i]; //rep(i, 0, n) A[i] = random(-10, 10); //rep(i, 0, n) B[i] = random(-10, 10); slow_state_solution(A, B, k); } else { int testcount; cin >> testcount; FOR(_z, 1, testcount) { if (_z % 10 == 0) cout << "OK: " << _z << endl; //TEST GENERATION int n = random(5000, 5000); int k = random(n/2, n/2+5); vi A(n), B(n); rep(i, 0, n) A[i] = random(-100, 100); rep(i, 0, n) B[i] = random(-100, 100); //END TEST GENERATION auto model = slow_state_solution(A, B, k); assert(model != -1); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; /*#include <atcoder/modint> using namespace atcoder; using mint = modint998244353; vector <mint> fac, inv; mint binom(int n, int k) { if (n < k || n < 0) return 0; return fac[n] * inv[k] * inv[n-k]; } void prep(int N) { fac.resize(N+1, 1); inv.resize(N+1, 1); for (int i=1; i<=N; ++i) fac[i] = fac[i-1] * i; inv[N] = fac[N].inv(); for (int i=N-1; i>0; --i) inv[i] = inv[i+1] * (i + 1); }*/ mt19937_64 rng(2137); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif const int maxn = 1000100; //Did you REAALLY consider sample tests? ll zlodzieje(vector <int> vec) { ll opt = 0; ll minpref = 0; ll pref = 0; for (int i=0; i<sz(vec); ++i) { pref += vec[i]; opt = max(opt, pref - minpref); minpref = min(minpref, pref); } return opt; } ll better(vector <int> vec) { ll opt = 0; ll curr = 0; trav(elem, vec) { curr += elem; opt = max(opt, curr); if (curr < 0) curr = 0; } return opt; } const ll INF = LLONG_MAX / 4; ll brut(vi A, vi B, int k) { int n = sz(A); int all = (1 << n); ll ukradna = INF; rep(mask, 0, all) { if (__builtin_popcount(mask) != k) continue; vi muzeum(n); rep(i, 0, n) { if (mask & (1 << i)) muzeum[i] = A[i]; else muzeum[i] = B[i]; } ukradna = min(ukradna, zlodzieje(muzeum)); assert(zlodzieje(muzeum) == better(muzeum)); } return ukradna; } /*ll solve(vector <ll> costs, vi init, int ile_wziac) { int n = sz(costs); //debug(init); while (ile_wziac > 0) { --ile_wziac; int best = -1; ll ukradna = INF; rep(i, 0, n) { if (costs[i] == INF) continue; init[i] += costs[i]; ll moze_byc = zlodzieje(init); init[i] -= costs[i]; if (moze_byc < ukradna) { ukradna = moze_byc; best = i; } } assert(best != -1); init[best] += costs[best]; costs[best] = INF; //debug(init); } return zlodzieje(init); }*/ struct stan { ll maks; ll psum; bool alice; stan* previous_state; stan(ll ma, ll ps, bool ali, stan*previous) : maks(ma), psum(ps), alice(ali), previous_state(previous) {} }; bool cmp(stan*ten, stan*other) { if (ten->maks != other->maks) return ten->maks < other->maks; if (ten->psum != other->psum) return ten->psum < other->psum; return ten->previous_state < other->previous_state; } vector <stan*> remove_suboptimal(vector <stan*> states) { if (states.empty()) return states; sort(all(states), cmp); // better score, smaller suffix -> better state overall vector <stan*> zostaja = {states[0]}; for (int i=1; i<sz(states); ++i) { if (zostaja.back()->psum <= states[i]->psum) continue; zostaja.push_back(states[i]); } return zostaja; } //a.insert(a.end(), b.begin(), b.end()); vector <stan*> add_element(vector <stan*> states, ll nowy, bool alice) { vector <stan*> result(sz(states), nullptr); for (int i=0; i<sz(states); ++i) { ll maks = states[i]->maks, psum = states[i]->psum; psum += nowy; if (psum < 0) psum = 0; maks = max(maks, psum); result[i] = new stan(maks, psum, alice, states[i]); } return result; } const int MAGIC = 10; ll slow_state_solution(vi A, vi B, int k) { int n = sz(A); vi inputA(n), inputB(n); rep(i, 0, n) inputA[i] = A[i], inputB[i] = B[i]; bool lifehack = false; if (k >= n - k) { lifehack = true; swap(A, B); k = n - k; } vector <vector<stan*>> best(k + 1); best[0].pb(new stan(0, 0, 0, nullptr)); rep(i, 0, n) { vector<vector<stan*>> nowy(k+1); FOR(j, 0, k) { int remains = n - i; if (j + remains < k) continue; auto alice = add_element(best[j], A[i], true); auto bob = add_element(best[j], B[i], false); if (j < k) nowy[j + 1].insert(nowy[j + 1].end(), all(alice)); nowy[j].insert(nowy[j].end(), all(bob)); } FOR(j, 0, k) best[j] = remove_suboptimal(nowy[j]); nowy.clear(); nowy.shrink_to_fit(); FOR(j, 0, k) { //bad state trimming if (sz(best[j]) >= MAGIC) best[j].resize(MAGIC); } } auto verify = [&](string s) -> ll { vector <int> vec(n); rep(i, 0, n) { if (s[i] == 'A') vec[i] = inputA[i]; else vec[i] = inputB[i]; } return better(vec); }; stan* ostatni = best[k][0]; ll wynik = ostatni -> maks; string odp = ""; FOR(steps, 1, n) { if (ostatni->alice) odp += 'A'; else odp += 'B'; ostatni = ostatni->previous_state; } reverse(all(odp)); if (lifehack) { rep(i, 0, n) { if (odp[i] == 'A') odp[i] = 'B'; else odp[i] = 'A'; } } //PRZEMYSL ASSERTY /*if (verify(odp) != wynik) { OUT("UPS"); }*/ cout << wynik << "\n"; cout << odp << "\n"; return wynik; } ll wzo(vi A, vi B, int k) { int n = sz(A); vll init(n); vll costs(n); rep(i, 0, n) init[i] = min(A[i], B[i]); int alice = 0; rep(i, 0, n) { if (A[i] <= B[i]) ++alice, init[i] = A[i]; else init[i] = B[i]; } if (alice < k) { //chcemy wiecej obrazow A rep(i, 0, n) { if (A[i] > B[i]) costs[i] = A[i] - B[i]; else costs[i] = INF; } } else { //chcemy wiecej obrazow B rep(i, 0, n) { if (A[i] <= B[i]) costs[i] = B[i] - A[i]; else costs[i] = INF; } } return 0; //return heura(costs, init, abs(k - alice)); } bool TEST = 0; int main() { boost; if (!TEST) { int n, k; cin >> n >> k; vi A(n), B(n); rep(i, 0, n) cin >> A[i]; rep(i, 0, n) cin >> B[i]; //rep(i, 0, n) A[i] = random(-10, 10); //rep(i, 0, n) B[i] = random(-10, 10); slow_state_solution(A, B, k); } else { int testcount; cin >> testcount; FOR(_z, 1, testcount) { if (_z % 10 == 0) cout << "OK: " << _z << endl; //TEST GENERATION int n = random(5000, 5000); int k = random(n/2, n/2+5); vi A(n), B(n); rep(i, 0, n) A[i] = random(-100, 100); rep(i, 0, n) B[i] = random(-100, 100); //END TEST GENERATION auto model = slow_state_solution(A, B, k); assert(model != -1); } } } |