제출 #302643

#제출 시각아이디문제언어결과실행 시간메모리
302643daniel920712카니발 티켓 (IOI20_tickets)C++14
55 / 100
1984 ms203288 KiB
#include "tickets.h" #include <vector> #include <algorithm> #include <set> using namespace std; struct A { int where; int con1,con2; }tt[1505]; vector < vector < int > > answer; vector < int > row; vector< vector<int> > all; vector < pair < int , pair < int , int > > > how; vector < int > uu2[1505]; set < int > uu[1505]; bool have[1505][1505]; bool have2[305][90005]; bool use[1505]={0}; long long DP[1505][1505]; long long DP2[305][90005]; long long con[1505][1505]; int a[1505]; int b[1505]; int N,M; long long F(int here,int con) { if(con>N/2) return -1e18; if(here==N&&con!=N/2) return -1e18; if(here==N) return 0; if(have[here][con]) return DP[here][con]; have[here][con]=1; DP[here][con]=max(F(here+1,con+1)+all[here][M-1],F(here+1,con)-all[here][0]); return DP[here][con]; } long long F3(int here,int con,int k) { long long t=0,i,j; if(con>N*k/2) return -1e18; if(here==N&&con!=N*k/2) return -1e18; if(here==N) return 0; if(have2[here][con]) return DP2[here][con]; have2[here][con]=1; DP2[here][con]=0; for(i=0;i<=k;i++) { t=0; for(j=0;j<i;j++) t-=all[here][j]; for(j=0;j<k-i;j++) t+=all[here][M-j-1]; DP2[here][con]=max(DP2[here][con],F3(here+1,con+i,k)+t); } return DP2[here][con]; } void F4(int here,int con,int k) { if(here==N) return; int i,j; long long t=0; for(i=0;i<=k;i++) { t=0; for(j=0;j<i;j++) t-=all[here][j]; for(j=0;j<k-i;j++) t+=all[here][M-j-1]; if(DP2[here][con]==F3(here+1,con+i,k)+t) { F4(here+1,con+i,k); for(j=0;j<i;j++) uu[here].insert(i); for(j=0;j<k-i;j++) uu[here].insert(M-j-1); } } } void F2(int here,int con) { if(here==N) return; if(F(here+1,con+1)+all[here][M-1]>F(here+1,con)-all[here][0]) { F2(here+1,con+1); answer[here][M-1]=0; } else { F2(here+1,con); answer[here][0]=0; } } bool cmp1(A a,A b) { return a.con1>b.con1; } bool cmp2(A a,A b) { return a.con2>b.con2; } long long find_maximum(int k,vector< vector<int> > x) { all=x; N=x.size(); M=x[0].size(); int i,j,l,t,ok,now,ll=0,rr; long long ans=0; for(i=0;i<M;i++) row.push_back(-1); for(i=0;i<N;i++) { answer.push_back(row); for(j=0;j<M;j++) { how.push_back(make_pair(x[i][j],make_pair(i,j))); if(x[i][j]<=1) ll++; } } if(k==1) { ans=F(0,0); F2(0,0); } else if(k==M) { sort(how.begin(),how.end()); for(i=0;i<N*M/2;i++) ans-=how[i].first; for(i=N*M/2;i<N*M;i++) ans+=how[i].first; for(i=0;i<N*M;i++) con[how[i].second.first][how[i].second.second]=i; for(i=0;i<N;i++) { a[i]=0; b[i]=M-1; } for(i=0;i<k;i++) { ll=0; rr=0; for(j=0;j<N;j++) use[j]=0; for(j=0;j<N;j++) { if(a[j]<k&&con[j][a[j]]>=N*M/2) { use[j]=1; rr++; answer[j][b[j]]=i; b[j]--; } if(b[j]>=0&&con[j][b[j]]<N*M/2) { use[j]=1; ll++; answer[j][a[j]]=i; a[j]++; } } for(j=0;j<N;j++) { if(use[j]) continue; if(ll<N/2) { use[j]=1; ll++; answer[j][a[j]]=i; a[j]++; } else { use[j]=1; rr++; answer[j][b[j]]=i; b[j]--; } } } } else if(ll==N*M) { //allocate_tickets(answer); ll=0; rr=0; t=0; for(i=0;i<N;i++) { a[i]=0; b[i]=k-1; t=0; for(j=0;j<M;j++) if(x[i][j]==0) t++; ll+=min(k,t); tt[i].con1=min(k,t); t=0; for(j=0;j<M;j++) if(x[i][j]==1) t++; rr+=min(k,t); tt[i].con2=min(k,t); tt[i].where=i; } ans=min(min(ll,rr),N*k/2); //printf("%d\n",ans); //printf("%d %d\n",ll,rr); //for(i=0;i<N;i++) for(j=0;j<k;j++) answer[i][j]=j; if(ll<rr) { ll=ans; sort(tt,tt+N,cmp1); t=0; for(i=0;i<N;i++) { b[i]=M-1; a[i]=0; } for(i=0;i<k;i++) { for(j=0;j<N;j++) { if(t<ans&&x[j][a[j]]==0) { t++; uu[j].insert(a[j]); a[j]++; } else { uu[j].insert(b[j]); b[j]--; } } } } else { rr=ans; sort(tt,tt+N,cmp2); t=0; for(i=0;i<N;i++) { b[i]=M-1; a[i]=0; } for(i=0;i<k;i++) { for(j=0;j<N;j++) { if(t<ans&&x[j][b[j]]) { t++; uu[j].insert(b[j]); b[j]--; } else { uu[j].insert(a[j]); a[j]++; } } } } ll=rr=0; for(i=0;i<N;i++) { for(auto j:uu[i]) { if(x[i][j]) ll++; else rr++; } } //printf("%d %d\n",ll,rr); for(i=0;i<N;i++) { while(ll<ans&&a[i]>=1&&b[i]>=0&&x[i][b[i]]) { ll++; rr--; a[i]--; uu[i].erase(a[i]); uu[i].insert(b[i]); b[i]--; } while(rr<ans&&b[i]<M-1&&a[i]<M&&x[i][a[i]]==0) { ll--; rr++; b[i]++; uu[i].erase(b[i]); uu[i].insert(a[i]); a[i]++; } } //printf("%d %d\n",ll,rr); //printf("aa\n"); how.clear(); ll=0; rr=0; for(i=0;i<N;i++) { for(auto j:uu[i]) { how.push_back(make_pair(x[i][j],make_pair(i,j))); uu2[i].push_back(j); //printf("%d %d\n",i,j); if(x[i][j]) ll++; else rr++; } } //printf("%d %d %d\n",ll,rr,ans); sort(how.begin(),how.end()); for(i=0;i<N*k;i++) { //printf("%d %d\n",how[i].second.first,how[i].second.second); con[how[i].second.first][how[i].second.second]=i; } for(i=0;i<N;i++) { a[i]=0; b[i]=k-1; } for(i=0;i<k;i++) { //printf("aa %d\n",i); ll=0; rr=0; for(j=0;j<N;j++) use[j]=0; for(j=0;j<N;j++) { if(a[j]<k&&con[j][uu2[j][a[j]]]>=N*k/2) { use[j]=1; rr++; answer[j][uu2[j][b[j]]]=i; b[j]--; } if(b[j]>=0&&con[j][uu2[j][b[j]]]<N*k/2) { use[j]=1; ll++; answer[j][uu2[j][a[j]]]=i; a[j]++; } } for(j=0;j<N;j++) { if(use[j]) continue; if(ll<N/2) { use[j]=1; ll++; answer[j][uu2[j][a[j]]]=i; a[j]++; } else { use[j]=1; rr++; answer[j][uu2[j][b[j]]]=i; b[j]--; } } } } else { ans=F3(0,0,k); F4(0,0,k); for(i=0;i<N;i++) { for(auto j:uu[i]) { how.push_back(make_pair(x[i][j],make_pair(i,j))); uu2[i].push_back(j); } } sort(how.begin(),how.end()); for(i=0;i<N*k;i++) con[how[i].second.first][how[i].second.second]=i; for(i=0;i<N;i++) { a[i]=0; b[i]=k-1; } for(i=0;i<k;i++) { ll=0; rr=0; for(j=0;j<N;j++) use[j]=0; for(j=0;j<N;j++) { if(a[j]<k&&con[j][uu2[j][a[j]]]>=N*k/2) { use[j]=1; rr++; answer[j][uu2[j][b[j]]]=i; b[j]--; } if(b[j]>=0&&con[j][uu2[j][b[j]]]<N*k/2) { use[j]=1; ll++; answer[j][uu2[j][a[j]]]=i; a[j]++; } } for(j=0;j<N;j++) { if(use[j]) continue; if(ll<N/2) { use[j]=1; ll++; answer[j][uu2[j][a[j]]]=i; a[j]++; } else { use[j]=1; rr++; answer[j][uu2[j][b[j]]]=i; b[j]--; } } } } allocate_tickets(answer); return ans; }

컴파일 시 표준 에러 (stderr) 메시지

tickets.cpp: In function 'long long int find_maximum(int, std::vector<std::vector<int> >)':
tickets.cpp:102:10: warning: unused variable 'l' [-Wunused-variable]
  102 |  int i,j,l,t,ok,now,ll=0,rr;
      |          ^
tickets.cpp:102:14: warning: unused variable 'ok' [-Wunused-variable]
  102 |  int i,j,l,t,ok,now,ll=0,rr;
      |              ^~
tickets.cpp:102:17: warning: unused variable 'now' [-Wunused-variable]
  102 |  int i,j,l,t,ok,now,ll=0,rr;
      |                 ^~~
#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...