Submission #998304

#TimeUsernameProblemLanguageResultExecution timeMemory
998304ArapakCarnival Tickets (IOI20_tickets)C++17
55 / 100
629 ms133752 KiB
// Author: Kajetan Ramsza #include "bits/stdc++.h" #include "tickets.h" using namespace std; #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const ll inf = ll(1e18) + 7; int n, m, k; vector<vi> answer; vector<pii> used; vector<vector<ll>> tickets; void update_used(vector<vector<ll>> &dp, int ind) { int curr = n / 2; for(int i=n-1;i>=0;i--) { if(dp[i+1][curr] == dp[i][curr] + tickets[i][used[i].second]) { answer[i][used[i].second] = ind; used[i].second--; } else { answer[i][used[i].first] = ind; used[i].first++; curr--; } } } ll solve_k1() { used.assign(n, {0, m-1}); vector<vector<ll>> dp(n+1, vector<ll>(n+1, -inf)); dp[0][0] = 0; ll result = 0; rep(ind,0,k) { rep(i,0,n) { dp[i+1][0] = -inf; rep(j,0,i+1) { dp[i+1][j] = max(dp[i+1][j], dp[i][j] + tickets[i][used[i].second]); dp[i+1][j+1] = dp[i][j] - tickets[i][used[i].first]; } } dbg(used); dbg(dp); update_used(dp, ind); result += dp[n][n / 2]; } return result; } ll solve() { vector<vi> ones(n); vector<vi> zeros(n); rep(i,0,n) rep(j,0,m) { if(tickets[i][j] == 1) ones[i].push_back(j); else zeros[i].push_back(j); } ll res = 0; rep(ind,0,k) { vector<pii> num(n); rep(i,0,n) num[i] = {sz(ones[i]), i}; sort(all(num)); rep(i,0,n) { int index = num[i].second; if((i < n/2 && sz(zeros[index])) || sz(ones[index]) == 0) { answer[index][zeros[index].back()] = ind; zeros[index].pop_back(); } else { answer[index][ones[index].back()] = ind; ones[index].pop_back(); res += i < n/2 ? -1 : 1; } } } return res; } ll solve_km() { vector<tuple<ll, int, int>> vals; rep(i,0,n) rep(j,0,m) vals.push_back({tickets[i][j], i ,j}); sort(all(vals)); ll result = 0; rep(ind,0,n*m) { auto [val, i, j] = vals[ind]; if(ind >= n*m / 2) { result += val; tickets[i][j] = 1; } else { result -= val; tickets[i][j] = 0; } } dbg(result); solve(); return result; } ll find_maximum(int k_, vector<vi> tickets_) { k = k_; n = tickets_.size(); m = tickets_[0].size(); tickets.assign(n, vector<ll>(m)); rep(i,0,n) rep(j,0,m) tickets[i][j] = tickets_[i][j]; answer.assign(n, vi(m, -1)); ll res; if(k == 1) res = solve_k1(); else if(k == m) res = solve_km(); else res = solve(); allocate_tickets(answer); return res; }
#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...