class BlurredDartboard { public: int minThrows(vector <int> p, int P) { int N=p.size(); VI vis(N, 0); int ma=-1; REP(i, N) { if(p[i]>0) { vis[p[i]-1]=1; ma=max(ma, p[i]); } } VI hid; REP(i, N) if(vis[i]==0) hid.PB(i+1); int hidsum = accumulate(ALL(hid), 0); sort(ALL(hid)); int ans=0; if(ma==-1 || hidsum > ma*hid.size()) { ans += P/hidsum*hid.size(); P -= P/hidsum*hidsum; } else { ans += P/ma; P -= P/ma*ma; } cout<<ans<<" "<<P<<endl; if(P==0) return ans; int lans=0; int PP=P; REP(i, hid.size()) { PP-=hid[i]; lans++; if(PP<=0) break; } if(PP>0) lans = 10000; cout<<lans<<endl; if(ma!=-1) lans = min(lans, (P+ma-1)/ma); cout<<lans<<" "<<ma<<endl; return ans+lans; } };
↓あとで通したやつ
vector <int> BB; VI w; map<PII, PII> memo; PII f(int bi, int wi) { PII key = MP(bi, wi); if(memo.count(key)) return memo[key]; if(wi==w.size()) return MP(0, 0); if(bi==BB.size()) return MP(-100000000, -100000000); PII rest = f(bi+1, wi+1); if(w[wi]==0) rest.first-=BB[bi]; else rest.first+=BB[bi]; rest.second+=BB[bi]; PII restB = f(bi+1, wi); //cout<<bi<<" "<<wi<<" "<<rest<<" "<<restB<<endl; return memo[key] = max(rest, restB); } class HeavyBooks { public: vector <int> findWeight(vector <int> B, vector <int> M) { memo = map<PII, PII>(); sort(ALL(B)); w = VI(M[0], 1); int to=0; REP(i, M.size()-1) { int mo = M[i+1]; for(int j=M[0]-1;j>=0;j--) { if(w[j]==1-to) { w[j]=to; mo--; if(mo==0) break; } } to=1-to; } cout<<w<<endl; BB=B; PII r = f(0, 0); cout<<r<<endl; vector<int> ans(2); ans[0]=-(r.first-r.second)/2; ans[1]=(r.first+r.second)/2; return ans; } };