Submission #770570

#TimeUsernameProblemLanguageResultExecution timeMemory
770570bleahbleahCarnival Tickets (IOI20_tickets)C++17
90 / 100
1857 ms2097152 KiB
#include <bits/stdc++.h> #include "tickets.h" #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long; using ld = long double; //#define int ll #define sz(x) ((int)(x).size()) using pii = pair<int,int>; using tii = tuple<int,int,int>; const int nmax = 1500 + 5; void partition(int k, vector<vector<int>> wsign) { int n = sz(wsign), m = sz(wsign[0]); vector<pii> freq(n, pii{0, 0}); vector<deque<int>> sorted(n); vector<vector<int>> rez(n, vector<int>(m, -1)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) if(wsign[i][j] == -1) sorted[i].emplace_back(j), freq[i].first++; for(int j = 0; j < m; j++) if(wsign[i][j] == 1) sorted[i].emplace_back(j), freq[i].second++; } for(int Q = 0; Q < k; Q++) { vector<int> checked(n); vector<int> atr(n); iota(all(atr), 0); sort(all(atr), [&](auto a, auto b) { return freq[a].first > freq[b].first; }); for(int T = 0, cnt = 0; cnt < n / 2 && T < n; T++) { int i = atr[T]; if(checked[i]) continue; if(freq[i].first > 0) { rez[i][sorted[i].front()] = Q; sorted[i].pop_front(); cnt++; checked[i] = 1; freq[i].first--; } } sort(all(atr), [&](auto a, auto b) { return freq[a].second > freq[b].second; }); for(int T = 0, cnt = 0; cnt < n / 2 && T < n; T++) { int i = atr[T]; if(checked[i]) continue; if(freq[i].second > 0) { rez[i][sorted[i].back()] = Q; sorted[i].pop_back(); cnt++; checked[i] = 1; freq[i].second--; } } } allocate_tickets(rez); return; } const ll infll = 1e18 + 5; const int nnmax = 300 + 5; ll S2(int k, vector<vector<int>> d) { int n = sz(d), m = sz(d[0]); vector<vector<ll>> dp(n + 1, vector<ll>(k * n / 2 + 2, -infll)); vector<vector<int>> with(n + 1, vector<int>(k * n / 2 + 2, 0)); reverse(all(d)); d.emplace_back(vector<int>()); reverse(all(d)); //for(int j = 1; j < sz(dp[0]); j++) dp[0][j] = -infll; dp[0][0] = 0; for(int i = 1; i <= n; i++) { vector<int> tmp = d[i]; sort(all(tmp)); vector<ll> cost(k + 1); ll tmpsum = 0; for(int i = 0; i < k; i++) { tmpsum += tmp[i]; cost[i + 1] -= tmpsum; } tmpsum = 0; for(int i = 0, j = sz(tmp) - 1; i < k; i++, j--) { tmpsum += tmp[j]; cost[k - i - 1] += tmpsum; } int lim = min(k * n / 2, i * k); for(int j = 0; j <= lim; j++) { int mnw = 0; dp[i][j] = -infll; for(int minus = 0; minus <= min(j, k); minus++) { if(dp[i - 1][j - minus] + cost[minus] > dp[i][j]) { mnw = minus; dp[i][j] = dp[i - 1][j - minus] + cost[minus]; } } with[i][j] = mnw; //cerr << dp[i][j] << ' '; } //cerr << '\n'; } //cerr << "finished\n"; int current = n / 2 * k; vector<vector<int>> wsign(n, vector<int>(m, 0)); for(int i = n; i > 0; i--) { vector<int> atr(m); iota(all(atr), 0); sort(all(atr), [&](int a, int b) { return d[i][a] < d[i][b]; }); //cerr << sz(with[i]) << ' ' << current << '\n'; int u = with[i][current]; current -= u; //cerr << u << '\n'; for(int j = 0; j < u; j++) //cerr << atr[j] << '\n', wsign[i - 1][atr[j]] = -1; for(int j = m - 1, t = 0; t < k - u; t++, j--) //cerr << atr[j] << '\n', wsign[i - 1][atr[j]] = 1; } //cerr << current << '\n'; partition(k, wsign); //cerr << "better\n"; ll sum = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) //cerr << wsign[i][j] << ' ', sum += wsign[i][j] * d[i + 1][j]; //cerr << '\n'; } return sum; } ll S3(int k, vector<vector<int>> d) { int n = sz(d), m = sz(d[0]); vector<pii> freq(n, pii{0, 0}); vector<deque<int>> sorted(n); vector<vector<int>> rez(n, vector<int>(m, -1)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) if(d[i][j] == 0) sorted[i].emplace_back(j), freq[i].first++; for(int j = 0; j < m; j++) if(d[i][j] == 1) sorted[i].emplace_back(j), freq[i].second++; } ll sum = 0; for(int Q = 0; Q < k; Q++) { vector<int> checked(n); vector<int> atr(n); iota(all(atr), 0); sort(all(atr), [&](auto a, auto b) { return freq[a].first > freq[b].first; }); int total = 0; for(int T = 0, cnt = 0; cnt < n / 2 && T < n; T++) { int i = atr[T]; if(checked[i]) continue; if(freq[i].first > 0) { rez[i][sorted[i].front()] = Q; sorted[i].pop_front(); cnt++; checked[i] = 1; freq[i].first--; } } sort(all(atr), [&](auto a, auto b) { return freq[a].second > freq[b].second; }); for(int T = 0, cnt = 0; cnt < n / 2 && T < n; T++) { int i = atr[T]; if(checked[i]) continue; if(freq[i].second > 0) { total++; rez[i][sorted[i].back()] = Q; sorted[i].pop_back(); cnt++; checked[i] = 1; freq[i].second--; } } for(int T = 0; T < n; T++) { int i = atr[T]; if(checked[i]) continue; if(freq[i].second > 0) total++, freq[i].second--; else freq[i].first--; rez[i][sorted[i].back()] = Q; sorted[i].pop_back(); checked[i] = 1; } sum += min(total, n - total); } allocate_tickets(rez); return sum; } ll S4(int k, vector<vector<int>> d) { vector<pii> values; int n = sz(d), m = sz(d[0]); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) values.emplace_back(i, j); sort(all(values), [&](auto a, auto b) { return d[a.first][a.second] < d[b.first][b.second]; }); vector<vector<int>> signs(n, vector<int>(m, 1)); for(int i = 0; i < n / 2 * k; i++) signs[values[i].first][values[i].second] = -1; partition(k, signs); ll sum = 0; for(auto [a, b] : values) { sum += d[a][b] * signs[a][b]; } return sum; } ll S1(int k, vector<vector<int>> d) { int n = sz(d), m = 1; vector<vector<int>> rez(n, vector<int>(m, 0)); allocate_tickets(rez); vector<int> v; for(auto a : d) for(auto x : a) v.emplace_back(x); sort(all(v)); ll sum = 0; for(int i = 0; i < n; i++) sum += v[i]; for(int i = 0; i < n / 2; i++) sum -= 2 * v[i]; return sum; } long long find_maximum(int k, std::vector<std::vector<int>> d) { bool ok = 1; for(auto a : d) for(auto x : a) ok &= (x == 0 || x == 1); if(ok) return S3(k, d); else if(sz(d[0]) == 1) return S1(k, d); else if(k == sz(d[0])) return S4(k, d); else return S2(k, d); } /** Anul asta se da centroid. -- Surse oficiale */
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...