↓あとで (passed system test in practice room)
class FoxPaintingBalls { public: long long theMax(long long R, long long G, long long B, int N) { ll tot = (ll)N*(N+1)/2; ll to3 = tot/3; ll lo=0, hi=(R+G+B)/tot+1; while(lo+1<hi) { ll mid = (lo+hi)/2; ll r=R-mid*to3; ll g=G-mid*to3; ll b=B-mid*to3; //cout<<mid<<" "<<r<<" "<<g<<" "<<b<<endl; if(r>=0 && g>=0 && b>=0 && (tot%3 ? (mid <= (r+g+b)/(tot%3)) : 1)) lo=mid; else hi=mid; } return lo; } };
class FoxAndFlowerShopDivOne { public: int theMaxFlowers(vector <string> F, int M) { int W=F[0].size(); int H=F.size(); // sum[y][x] is sum of [0, x) x [0, y) VVI sum(H+1, VI(W+1)); VVI dif(H+1, VI(W+1)); REP(x, W) REP(y, H) { sum[y+1][x+1] = (F[y][x]!='.' ? 1 : 0) + sum[y][x+1]+sum[y+1][x]-sum[y][x]; dif[y+1][x+1] = (F[y][x]=='L' ? 1 : (F[y][x]=='P' ? -1 : 0)) + dif[y][x+1]+dif[y+1][x]-dif[y][x]; } //cout<<sum<<endl; //cout<<dif<<endl; int ans = -1; REP(X, W) { map<int, int> hi0, hi1; // [0, X) x [0, H) for(int x0=0;x0<X;x0++) { for(int x1=x0;x1<X;x1++) { // [x0, x1] x [y0, y1] for(int y0=0;y0<H;y0++) { for(int y1=y0;y1<H;y1++) { int S = sum[y1+1][x1+1] - sum[y1+1][x0] - sum[y0][x1+1] + sum[y0][x0]; int D = dif[y1+1][x1+1] - dif[y1+1][x0] - dif[y0][x1+1] + dif[y0][x0]; hi0[D] = max(hi0[D], S); //cout<<MP(x0, MP(x1, MP(y0, y1)))<<" "<<S<<" "<<D<<endl; } } } } // [X, W) x [0, H) for(int x0=X;x0<W;x0++) { for(int x1=x0;x1<W;x1++) { // [x0, x1] x [y0, y1] for(int y0=0;y0<H;y0++) { for(int y1=y0;y1<H;y1++) { int S = sum[y1+1][x1+1] - sum[y1+1][x0] - sum[y0][x1+1] + sum[y0][x0]; int D = dif[y1+1][x1+1] - dif[y1+1][x0] - dif[y0][x1+1] + dif[y0][x0]; hi1[D] = max(hi1[D], S); } } } } FOR(e0, hi0) FOR(e1, hi1) if(abs(e0->first+e1->first)<=M) ans=max(ans, e0->second+e1->second); } REP(Y, H) { map<int, int> hi0, hi1; // [0, W) x [0, Y) for(int x0=0;x0<W;x0++) { for(int x1=x0;x1<W;x1++) { // [x0, x1] x [y0, y1] for(int y0=0;y0<Y;y0++) { for(int y1=y0;y1<Y;y1++) { int S = sum[y1+1][x1+1] - sum[y1+1][x0] - sum[y0][x1+1] + sum[y0][x0]; int D = dif[y1+1][x1+1] - dif[y1+1][x0] - dif[y0][x1+1] + dif[y0][x0]; hi0[D] = max(hi0[D], S); } } } } // [0, W) x [Y, H) for(int x0=0;x0<W;x0++) { for(int x1=x0;x1<W;x1++) { // [x0, x1] x [y0, y1] for(int y0=Y;y0<H;y0++) { for(int y1=y0;y1<H;y1++) { int S = sum[y1+1][x1+1] - sum[y1+1][x0] - sum[y0][x1+1] + sum[y0][x0]; int D = dif[y1+1][x1+1] - dif[y1+1][x0] - dif[y0][x1+1] + dif[y0][x0]; hi1[D] = max(hi1[D], S); } } } } FOR(e0, hi0) FOR(e1, hi1) if(abs(e0->first+e1->first)<=M) ans=max(ans, e0->second+e1->second); } return ans; } };