/* □駒□駒□ 駒□□□駒 □□×□□ 駒□□□駒 □駒□駒□ K=2 になるようなサブエリア例 □■■■□ □■■■□ □■■■□ □□□□□ □□□□□ K=2 になるようなサブエリア例 □□■■■ □□■■■ □□■■■ □□□□□ □□□□□ K=2 になるようなサブエリア例 □□■■□ □□■■□ □□■■□ □□■■□ □□■■□ */
(あとで)
↓あとで (accepted in practice)
class HyperKnight { public: long long countCells(int A, int B, int NR, int NC, int K) { VVI pat(5, VI(5, 0)); int eX[] = {0,0,1,1,3,3,4,4}; int eY[] = {1,3,0,4,0,4,1,3}; REP(i, 8) pat[eY[i]][eX[i]]=1; //cout<<pat<<endl; if(B>A) swap(A, B); ll ans = 0; REP(y0, 5) { REP(x0, 5) { RANGE(y1, y0, 5) { RANGE(x1, x0, 5) { if(x1<2||2<x0||y1<2||2<y0) continue; int co=0; RANGE(y, y0, y1+1) RANGE(x, x0, x1+1) co+=pat[y][x]; //cout<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<" ++ "<<co<<endl; if(co==K) { //cout<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<" --- "<<endl; int tb[] = {0, B, A, A+1}; int a0=x0, a1=x1, N=NC; ll cc[2]; int ok=1; REP(loop, 2) { int i0 = abs(a0-2); int i1 = abs(a1-2); ll cmin0 = tb[i0]; ll cmin1 = tb[i1]; ll cmax0 = tb[i0+1]-1; ll cmax1 = tb[i1+1]-1; // ながさ in [c0, c1] ll c0 = cmin0 + 1 + cmin1; ll c1 = cmax0 + 1 + cmax1; if(!(i0==2||i1==2)) { if(!(c0<=N&&N<=c1)) ok=0; } if(c0>N) ok=0; c1 = min((ll)N, c1); //cout<<c0<<" "<<c1<<endl; ll c = 0; if(i0==2&&i1==2) c = N-c0+1; else c = c1-c0+1; a0=y0, a1=y1, N=NR; cc[loop] = c; } if(ok) { ans += cc[0] * cc[1]; //cout<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<" --- "<<cc[0]<<" "<<cc[1]<<endl; } } } } } } return ans; } };
↓あとで (accepted in practice)
class FoxAndMountainEasy { public: string possible(int n, int h0, int hn, string HI) { if(abs(h0-hn)>n) return "NO"; if( (n-abs(h0-hn))%2 != 0 ) return "NO"; int U=0, D=0, UD=0; if(h0<hn) U=abs(h0-hn); else D=abs(h0-hn); UD = n-abs(h0-hn); U += UD/2; D += UD/2; int u=count(ALL(HI), 'U'); int d=count(ALL(HI), 'D'); if(U<u || D<d) return "NO"; int Min=0; int pos=0; FOR(e, HI) { if(*e=='D') pos--; if(*e=='U') pos++; Min = min(Min, pos); } //if(abs(Min) > max(h0, hn)) return "NO"; // だめ if(abs(Min) > h0+U-u) return "NO"; // OK return "YES"; } };
(あとで)
↓あとで (accepted in practice)
#define INF (1<<30) int dp[2][1010]; int main() { ll H,W,X,Y; while(cin>>H>>W>>X>>Y) { REP(col, 2) REP(x, 1010) dp[col][x]=INF; VVI co(2, VI(W)); REP(i, H) { string s; cin>>s; REP(x, s.size()) co[0][x]+=s[x]=='.', co[1][x]+=s[x]!='.'; } REP(x, W) { REP(col, 2) { RANGE(i, X, Y+1) { int v = 0; if(x-i>=0) v += dp[1-col][x-i]; if(x-i+1>=0) { RANGE(j, x-i+1, x+1) v += co[col][j]; dp[col][x] = min(dp[col][x], v); } } } } cout<<min(dp[0][W-1], dp[1][W-1])<<endl; } return 0; }
(あとで)
↓実装練習 (accepted in practice)
int cum[100010]; // i is # of '[' in S[k] for k in [0, i) int count(int L, int R) { return cum[R+1]-cum[L]; } int main() { //ios::sync_with_stdio(false); string S; while(cin>>S) { cum[0] = 0; REP(i, S.size()) { cum[i+1] = cum[i] + (S[i]=='['); } deque<pair<char, int> > mo; int N=S.size(); int L=-1, R=-1; int co = 0; int start = 0; REP(i, N) { char op = S[i]==']' ? '[' : '('; if(S[i]=='[' || S[i]=='(') { mo.PB(MP(S[i], i)); } else { if(mo.size() && mo.back().first==op) { mo.pop_back(); int NL = start; if(mo.size()) NL = mo.back().second + 1; int NR = i; int nco = count(NL, NR); if( co < nco ) { co = nco; L=NL; R=NR; } } else { mo.clear(); start = i+1; } } } cout<<co<<endl; if(co>0) cout<<S.substr(L, R-L+1)<<endl; } return 0; }
(あとで)
↓実装練習 (passed system test in practice room)
#define MOD 555555555 int comb[2500][2500]; class XorBoard { public: int count(int H, int W, int RC, int CC, int S) { // nCm = n-1Cm-1 + n-1Cm REP(n, 2500) comb[n][0] = 1; RANGE(n, 1, 2500) RANGE(m, 1, 2500) comb[n][m] = ((ll)comb[n-1][m-1] + comb[n-1][m]) % MOD; ll ans = 0; REP(ro, H+1) if(ro<=RC && (RC-ro)%2==0) REP(co, W+1) if(co<=CC && (CC-co)%2==0) if(W*ro+H*co-2*ro*co==S) { ll lans = (ll)comb[H][ro] * comb[W][co] % MOD * comb[(RC-ro)/2 + H-1][H-1] % MOD * comb[(CC-co)/2 + W-1][W-1] % MOD; ans = (ans + lans) % MOD; } return ans; } };