이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// 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_1() {
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 if(tickets[i][j] == 0)
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_1();
return result;
}
ll solve() {
vector<vector<ll>> dp(n+1, vector<ll>(n*k+1, -inf));
vector<vector<ll>> curr_dp(n, vector<ll>(k+1, 0));
dp[0][0] = 0;
rep(i,0,n) {
rep(j,m-k,m) curr_dp[i][0] += tickets[i][j];
rep(j,0,k) curr_dp[i][j+1] = curr_dp[i][j] - tickets[i][m - k + j] - tickets[i][j];
dbg(curr_dp[i]);
rep(j,0,n*k+1)
rep(o,0,min(k,j)+1)
dp[i+1][j] = max(dp[i+1][j], curr_dp[i][o] + dp[i][j-o]);
}
int curr = n * k / 2;
for(int i=n-1;i>=0;i--) {
int index = -1;
rep(j,0,min(k, curr) + 1)
if(dp[i+1][curr] == curr_dp[i][j] + dp[i][curr - j]) {
index = j;
break;
}
dbg(index);
curr -= index;
rep(j,0,index) tickets[i][j] = 0;
rep(j,index,m-k+index) tickets[i][j] = -1;
rep(j,m-k+index,m) tickets[i][j] = 1;
}
solve_1();
return dp[n][n*k / 2];
}
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 if(n <= 80 && m <= 80) res = solve();
else res = solve_1();
allocate_tickets(answer);
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |