This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include "tickets.h"
#define MAXN 1507
using namespace std;
const long long inf=1e15;
int n,m,sol[MAXN],k,rest[MAXN],br,maxs,ind[MAXN];
long long pref[MAXN][MAXN],suff[MAXN][MAXN];
pair<int,int> s[MAXN][MAXN];
vector<int> curr;
vector< vector<int> > res;
int li[MAXN][MAXN*20],tim,used[MAXN];
long long dp[MAXN][MAXN*20],ans;
long long ff(int pos,int balance){
if(balance<0 or balance>20*m)return -inf;
if(pos==-1 and balance==10*m)return 0;
else if(pos==-1)return -inf;
if(li[pos][balance]==tim)return dp[pos][balance];
li[pos][balance]=tim;
dp[pos][balance]=-inf;
for(int i=0;i<=k;i++){
dp[pos][balance]=max(dp[pos][balance],ff(pos-1,balance-i+k-i)+suff[ind[pos]][k-i]-pref[ind[pos]][i]);
}
return dp[pos][balance];
}
void solve(int pos,int balance){
if(pos==-1)return;
for(int i=0;i<=k;i++){
if(ff(pos,balance)==ff(pos-1,balance-i+k-i)+suff[ind[pos]][k-i]-pref[ind[pos]][i]){
sol[ind[pos]]=i; rest[ind[pos]]=k-i;
solve(pos-1,balance-i+k-i);
return;
}
}
}
long long find_maximum(int K,vector< vector<int> > x){
n=int(x.size()); m=int(x[0].size()); k=K;
res.resize(n);
for(int i=0;i<n;i++){
res[i].resize(m); ind[i]=i;
for(int f=0;f<m;f++){
s[i][f]={x[i][f],f};
res[i][f]=-1;
}
sort(s[i],s[i]+m);
for(int f=0;f<m;f++){
pref[i][f+1]=s[i][f].first;
pref[i][f+1]+=pref[i][f];
}
for(int f=m-1;f>=0;f--){
suff[i][m-f]=s[i][f].first;
suff[i][m-f]+=suff[i][m-f-1];
}
}
random_shuffle(ind,ind+n);
tim++; solve(n-1,10*m);
ans=ff(n-1,10*m);
for(int i=0;i<k;i++){
br=n/2; tim++;
for(int f=0;f<n/2;f++){
maxs=n;
for(int w=0;w<n;w++){
if(sol[w]>sol[maxs] and used[w]!=tim)maxs=w;
}
res[maxs][s[maxs][sol[maxs]-1].second]=i;
sol[maxs]--; used[maxs]=tim;
}
for(int f=0;f<n;f++){
if(used[f]!=tim){
res[f][s[f][m-rest[f]].second]=i;
rest[f]--;
}
}
}
allocate_tickets(res);
return ans;
}
/*
int main(){
cout<<find_maximum(2, {{0, 2, 5},{1, 1, 3}})<<"\n";
}
*/
# | 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... |