class SkiResorts { public: long long minCost(vector <string> R, vector <int> A) { int N=R.size(); ll INF = 1LL<<60; VI po(N*N, INF); priority_queue<PII, vector<PII>, greater<PII> > q; REP(j, N) { po[j]=abs(A[j]-A[0]); q.push(MP(po[j], j)); } int end=0; ll ans = INF; while(q.size()) { ll pot = q.top().first; int cur = q.top().second; int cur_n = cur/N; int cur_h = cur%N; q.pop(); if(pot > po[cur]) continue; end += cur_n==N-1; if(end==N) break; REP(i, N) { if(R[cur_n][i]=='Y') { REP(j, N) { if(A[cur_h] < A[j]) continue; int cost = abs(A[j]-A[i]); if(po[cur] + cost < po[i*N+j]) { po[i*N+j] = po[cur] + cost; q.push(MP(po[i*N+j], i*N+j)); } } } } } REP(i, N) ans=min(ans, po[(N-1)*N+i]); return ans==INF ? -1 : ans; } };
class EllysBulls { public: string getNumber(vector <string> G, vector <int> B) { int N=G.size(), K=G[0].size(); map<VI, PII> rests; int K0=K/2, K1=K-K0; VI ten(1, 1); REP(i, 6) ten.PB(ten.back()*10); cout<<ten<<endl; cout<<K0<<" "<<K1<<endl; //return ""; REP(v, ten[K0]) { //cout<<"p1 "<<v<<endl; char buf[20]; sprintf(buf, "%0*d", K0, v); //cout<<"buf: "<<string(buf)<<endl; VI rest(N); int ok=1; REP(gi, N) { int match=0; REP(k, K0) match += buf[k]==G[gi][k]; if(B[gi]-match<0) {ok=0;break;} rest[gi] = B[gi]-match; } if(ok) { rests[rest].first++; rests[rest].second = v; } } //cout<<rests<<endl; string ans; REP(v, ten[K1]) { char buf[20]; sprintf(buf, "%0*d", K1, v); VI key(N); REP(gi, N) { int match=0; REP(k, K1) match += buf[k]==G[gi][K0+k]; key[gi] = match; } //cout<<"key: "<<key<<endl; if(rests.count(key)) { //cout<<"p2 count "<<v<<" "<<rests.count(key)<<endl; if(rests[key].first > 1) return "Ambiguity"; if(ans.size()) return "Ambiguity"; char buf[30]; sprintf(buf, "%0*d", K0, rests[key].second); sprintf(&buf[K0], "%0*d", K1, v); ans = string(buf); } } if(ans.size()==0) return "Liar"; return ans; } };
class EllysFigurines { public: int getMoves(vector<string> B, int R, int C) { int W=B[0].size(); int H=B.size(); int ans = 1<<30; REP(bit, 1<<H) { int lans=0; int rmy[20]={}; int rest[20]={}; REP(y, H) { if((bit>>y)&1) { lans++; REP(i, R) if(y+i<H) rmy[y+i]=1; } if(!rmy[y]) REP(x, W) if(B[y][x]=='X') rest[x]=1; } REP(x, W) { if(rest[x]) { lans++; x+=C-1; } } ans=min(ans, lans); } return ans; } };
class EllysReversals { public: int getMin(vector <string> W) { int N=W.size(); map<string, int> hi; REP(i, N) { vector<string> ww; REP(j, W[i].size()/2*2) { string ss; ss.PB(W[i][j]); ss.PB(W[i][j+1]); j++; sort(ALL(ss)); ww.PB(ss); } sort(ALL(ww)); string sn; FOR(e, ww) sn+=*e; if(W[i].size() & 1) sn+=W[i][W[i].size()-1]; hi[sn]++; } cout<<hi<<endl; int ans = 0; FOR(e, hi) if(e->second & 1) ans++; return ans; } };
class TheFrog { public: double getMinimum(int D, vector <int> L, vector <int> R) { double ans = D*2; REP(i, L.size()) { //50 for(ll div=R[i];div>=1;div--) {//30000 double mid=(double)R[i]/div; // mid は二分探索のなごり int ok=1; REP(j, L.size()) { // EPS つけたらこっちもOKだった //double p0 = floor(L[j]/mid+1e-9)*mid; //if(p0 + mid < R[j]-1e-9) {ok=0;break;} ll lp1 = (L[j]*div/R[i]+1) * R[i]; if(lp1<R[j]*div) {ok=0;break;} } if(ok) { ans=min(ans, mid); break; } } } return ans; } };
#define ABS(x) (x>=0 ? x : (-x)) class RobotHerb { public: void go(vector<int>& A, ll& x, ll& y, ll& dir) { ll dx[] = {0,1,0,-1}; ll dy[] = {1,0,-1,0}; REP(i, A.size()) { x+=dx[dir]*A[i]; y+=dy[dir]*A[i]; dir = (dir+A[i]%4) % 4; } } long long getdist(int T, vector <int> A) { ll x4=0,y4=0,dir=0; REP(i, 4) go(A, x4, y4, dir); ll x=x4*(T/4),y=y4*(T/4); cout<<x4<<" "<<y4<<endl; REP(i, T%4) go(A, x, y, dir); cout<<x<<" "<<y<<endl; //return llabs(x)+llabs(y); return ABS(x)+ABS(y); } };