vector<VI> subsets(VI &S) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<VI> result; if (S.size() == 0) { result.push_back(VI()); return result; } int first = S.front(); VI child(S.begin() + 1, S.end()); vector<VI> subsetsChild = subsets(child); vector<VI>::iterator iter = subsetsChild.begin(); while (iter < subsetsChild.end()) { VI &set = *iter; sort(set.begin(), set.end()); result.push_back(set); set.push_back(first); sort(set.begin(), set.end()); result.push_back(set); iter++; } return result; }
void go(VI as, int le, int ri) { assert((int)as.size()==ri-le+1); if(as.empty()) return; sort(all(as)); int x=as.front(); int cnt=0; VI s1, s2; for(int y: as) { if(y==x) continue; if(g[y][x]) ++cnt, s1.PB(y); else s2.PB(y); } ans[x]=le+cnt; go(s1, le, le+(int)s1.size()-1); go(s2, ri-(int)s2.size()+1, ri); }
vector<int> theCards(int N, string C) { sieve(N); int K=SZ(C); VI res(K,-1); REP(i,K) { VI S; FORE(n,1,N) if(P[n]==(C[i]=='R')) { bool lok=true; int k=n; for(int j=i-1; j>=0; --j) { --k; if(C[j]=='R') { while(k>=1&&!P[k]) --k; } else { while(k>=1&&P[k]) --k; } if(k<1) { lok=false; break; } } k=n; bool rok=true; for(int j=i+1; j<K; ++j) { ++k; if(C[j]=='R') { while(k<=N&&!P[k]) ++k; } else { while(k<=N&&P[k]) ++k; } if(k>N) { rok=false; break; } } // dump3(i,n,k); // dump4(i,n,lok,rok); if(lok&&rok) S.push_back(n); } if(SZ(S)==1) res[i]=S.front(); }