bool isOk(VI a, VI b) { memset(ok, 0, sizeof(ok)); if(binary_search(a.begin(), a.end(), 6)) { a.push_back(9); } if(a.back()==9) { a.push_back(6); } if(binary_search(b.begin(), b.end(), 6)) { b.push_back(9); } if(b.back()==9) { b.push_back(6); } VI::iterator i, j; for(i=a.begin(); i!=a.end(); ++i) { for(j=b.begin(); j!=b.end(); ++j) { ok[(*i)*10+(*j)]=true; ok[(*j)*10+(*i)]=true; } } bool ret=true; for(int in=1; in<10; ++in) { if(!ok[in*in]) { ret=false; } } return ret; }
VI MULTI(VI X, VI Y) { int N = X.size(), N2 = N / 2; VI R(N * 2, 0); if (N > 100) { VI X2(X.begin() + N2, X.end()); VI Y2(Y.begin() + N2, Y.end()); X.resize(N2); Y.resize(N2); VI A = MULTI(X, Y); VI B = MULTI(X2, Y2); for (int i = 0; i < N2; i++) X2[i] += X[i], Y2[i] += Y[i]; VI C = MULTI(X2, Y2); for (int i = 0; i < A.size(); i++) R[i] += A[i], C[i] -= A[i]; for (int i = 0; i < B.size(); i++) R[i + N2 * 2] += B[i], C[i] -= B[i]; for (int i = 0; i < C.size(); i++) R[i + N2] += C[i]; } else { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) R[i + j] += X[i] * Y[j]; } return R; }
void filterDataSet(VS &data, const int scenario, const bool includeClass) { VI indices; if (scenario == 2) { VI sc3 = {11, 13, 19, 20, 21, 22, 23, 24, 25, 26}; indices.insert(indices.begin(), sc3.begin(), sc3.end()); } if (scenario >= 1) { VI sc2 = {2, 3, 4, 5, 6, 7, 8, 9, 10}; indices.insert(indices.begin(), sc2.begin(), sc2.end()); } VI sc1 = {1, 12, 14, 15, 16, 17, 18}; indices.insert(indices.begin(), sc1.begin(), sc1.end()); for (int i = 0; i < data.size(); i++) { string line = data[i]; VS values = splt(line); string newLine; for (int j = 0; j < indices.size(); j++) { newLine.append(values[indices[j] - 1]); if (j < indices.size() - 1) { newLine.append(","); } } if (includeClass) { newLine.append(","); newLine.append(values[iqCol]); } // store new data data[i] = newLine; } }
int main(int argc, char** argv) { constexpr int default_n = 100; int n = argc > 1 && atoi(argv[1]) > default_n ? atoi(argv[1]) : default_n; VI va; VI vb; for (int i = 0; i < n; ++i) va.push_back(generate_x(i + 1)); for (int i = 0; i < n; ++i) vb.push_back(generate_x(i + 1)); sort(va.begin(), va.end()); sort(vb.begin(), vb.end()); auto start_time = steady_clock::now(); auto r1 = find_common(va, vb); cout << "use " << duration_cast<microseconds>(steady_clock::now() - start_time).count() << " microseconds\n"; start_time = steady_clock::now(); auto r2 = find_common1(va, vb); cout << "use " << duration_cast<microseconds>(steady_clock::now() - start_time).count() << " microseconds\n"; if (r1 != r2) cout << "error\n"; return 0; }
void suffixArray(VI &T, VI &SA, int n, int K) { int n0 = (n + 2) / 3, n1 = (n + 1) / 3, n2 = n / 3, n02 = n0 + n2; VI R(n02+3), SA12(n02+3), R0(n0), SA0(n0); for (int i = 0, j = 0; i < n + (n0 - n1); i++) if (i % 3 != 0) R[j++] = i; radixPass(R, SA12, T.begin() + 2, n02, K); radixPass(SA12, R, T.begin() + 1, n02, K); radixPass(R, SA12, T.begin(), n02, K); int name = 0, c0 = -1, c1 = -1, c2 = -1; for (int i = 0; i < n02; i++) { if (T[SA12[i]] != c0 || T[SA12[i] + 1] != c1 || T[SA12[i] + 2] != c2) { name++; c0 = T[SA12[i]]; c1 = T[SA12[i] + 1]; c2 = T[SA12[i] + 2]; } if (SA12[i] % 3 == 1) { R[SA12[i] / 3] = name; } else { R[SA12[i] / 3 + n0] = name; } } if (name < n02) { suffixArray(R, SA12, n02, name); for (int i = 0; i < n02; i++) R[SA12[i]] = i + 1; } else for (int i = 0; i < n02; i++) SA12[R[i] - 1] = i; for (int i = 0, j = 0; i < n02; i++) if (SA12[i] < n0) R0[j++] = 3 * SA12[i]; radixPass(R0, SA0, T.begin(), n0, K); for (int p = 0, t = n0 - n1, k = 0; k < n; k++) { #define GetI() (SA12[t] < n0 ? SA12[t] * 3 + 1 : (SA12[t] - n0) * 3 + 2) int i = GetI(); // pos of current offset 12 suffix int j = SA0[p]; // pos of current offset 0 suffix if (SA12[t] < n0 ? // different compares for mod 1 and mod 2 suffixes leq(T[i], R[SA12[t] + n0], T[j], R[j / 3]) : leq(T[i], T[i + 1], R[SA12[t] - n0 + 1], T[j], T[j + 1], R[j / 3 + n0])) { // suffix from SA12 is smaller SA[k] = i; t++; if (t == n02) // done --- only SA0 suffixes left for (k++; p < n0; p++, k++) SA[k] = SA0[p]; } else { // suffix from SA0 is smaller SA[k] = j; p++; if (p == n0) // done --- only SA12 suffixes left for (k++; t < n02; t++, k++) SA[k] = GetI(); } } }
int getMinimum(vector <int> lines) { target = lines; memset(memo, -1, sizeof(memo)); for(int i=0;i<size(lines);i++) ls.pb(lines[i]); ls.pb(0); sort(ls.begin(),ls.end()); ls.erase(unique(ls.begin(), ls.end()), ls.end()); return go(0,0) - 1; }
bool solve(int n, int k) { if (k == 1) return true; if (k > MAXK) return false; if (fact[k] > n) return false; VI v; int N = n; for (VI::iterator it = primes.begin(); it != primes.end() && (*it) * (*it) <= N; ++it) if (N % *it == 0) { v.push_back(0); while (N % *it == 0) { ++v.back(); N /= *it; } } if (N > 1) v.push_back(1); sort(v.begin(), v.end()); reverse(v.begin(), v.end()); k -= (int) v.size() + 1; for (VI::iterator it = v.begin(); it != v.end(); ++it) *it -= 1; while (!v.empty() && v.back() == 0) { v.pop_back(); } if (k <= 0) return true; if (DBG + 0) { cerr << "BEGIN!!!!!\n"; for (VI::iterator it = v.begin(); it != v.end(); ++it) cerr << *it << " -- "; cerr << endl; cerr << k << endl; } VI w(v.size(), 1); bool found = walk1(v, w, 0, k/*,set<VI>()*/); if (!found) { if (v.size() >= 3 && v[2] >= 3) { v[0] -= 1; v[1] -= 1; v[2] -= 1; found = walk1(v, w, 0, k - 1/*,set<VI>()*/); } } return found; }
// targets 至少有一行,每行必有元素是前提 VM TARGETSEARCH::findpath (INFOGO * infop, TARGETS tgs) { VM vm; BITB b, bb; b = NULL_BB; // for (int i = 0; i < tgs.nut.size (); ++i) { bb = FULL_BB; for (int j = 0; j < tgs.nut[i].size (); ++j) bb &= scope (infop, tgs.nut[i][j]); b |= bb; } //-// 此处利用定式库的走步频率来排序 VI allvi (BS * BS, 0), vi, idsort; COLOR trn = tgs.nut[0][0].passtimes ? infop->getlastclr () : infop->turn (); for (int i = 0; i < ZOBRIST::ZOBRIST_KIND; ++i) { vi = infop->getf_zobrist (i, b, trn); transform (vi.begin (), vi.end (), allvi.begin (), allvi.begin (), plus < int >()); } ___ASSERT (allvi.size () == BS * BS); idsort = indexsort (allvi); for (int i = BS * BS - 1; i >= 0; --i) if (allvi[idsort[i]]) vm.push_back (make_pair (i2pos (idsort[i]), trn)); else break; if (!vm.empty ()) return vm; //-// 此处利用模式库的走步频率来排序 //g_learn.setpatfreq(infop); int m = 3; vi = infop->getf_patfreq (m, b, trn); idsort = indexsort (vi); for (int i = BS * BS - 1; i >= 0; --i) if (vi[idsort[i]] > 1) { vm.push_back (make_pair (i2pos (idsort[i]), trn)); } else break; if (!vm.empty ()) return vm; else { clog << "no match with lib_zobrist and lib_pemis ?!" << endl; return vm; } }
int compress(VI& x1,VI& x2, int W){ VI result; REP(i,SZ(x1))for(int d = -1; d <= 1 ;d++){ int x1n = x1[i]+d; int x2n = x2[i]+d; if(0 <= x1n && x1n < W) result.PB(x1n); if(0 <= x2n && x2n < W) result.PB(x2n); } sort(result.begin(), result.end()); result.erase(unique(result.begin(), result.end()), result.end()); REP(i,SZ(x1)){ x1[i] = (int)(find(ALL(result),x1[i]) - result.begin()); x2[i] = (int)(find(ALL(result),x2[i]) - result.begin()); }
void countingSort(int H) { auto vrank = [&](int i) { return SA[i]+H<N ? RA[SA[i]+H]+1 : 0; }; int maxRank = *max_element(RA.begin(), RA.end()); VI nSA(N); VI freq(maxRank + 2); for (int i = 0; i < N; ++i) freq[vrank(i)]++; for (int i = 1; i < freq.size(); ++i) freq[i] += freq[i-1]; for (int i = N-1, p, m; i >= 0; --i) nSA[--freq[vrank(i)]] = SA[i]; copy(nSA.begin(), nSA.end(), SA.begin()); }
VI LongestIncreasingSubsequence(VI v) { VPII best; VI dad(v.size(), -1); for (int i = 0; i < v.size(); i++) { #ifdef STRICTLY_INCREASNG PII item = make_pair(v[i], 0); VPII::iterator it = lower_bound(best.begin(), best.end(), item); item.second = i; #else PII item = make_pair(v[i], i); VPII::iterator it = upper_bound(best.begin(), best.end(), item); #endif if (it == best.end()) { dad[i] = (best.size() == 0 ? -1 : best.back().second); best.push_back(item); } else { dad[i] = dad[it->second]; *it = item; } } VI ret; for (int i = best.back().second; i >= 0; i = dad[i]) ret.push_back(v[i]); reverse(ret.begin(), ret.end()); return ret; }
void update(char oj,int difpos) { VI::iterator it,it2; for(it=pos.begin();it!=pos.end();) { // printf("*it=%d,difpos=%d\n",*it,difpos); if(*it+difpos<alen && *it+difpos>=0 && a[*it+difpos]==oj) { if(difpos==-1) *it--; it++; } else { // printf("before erase:%d\n",*it); // ans--; // it2=it; // it++; // pos.erase(it2); // printf("after erase:%d it==end()?:%d\n",*it,it==pos.end()); // printf("it2:after erase:%d it==end()?:%d\n",*it2,it2==pos.end()); ans--; // printf("before erase:%d\n",*it); pos.erase(it); // printf("after erase:%d it==end()?:%d\n",*it,it==pos.end()); } } }
int main() { int n; while(cin>>n) { VI ar; int k; // fill(pr,0); for( int i=0; i<n; i++ ) { cin>>k; ar.PB(k); } sort(ar.begin(),ar.end()); int val=0; for( int i=ar.size()-1; i>=0; i-- ) { //debug(i); val+=ar[i]; //debug(val); int sum=0; for( int j=0; j<i; j++ ) { sum+=ar[j]; // debug(sum); } if( sum < val ) {cout<<n-i<<endl;break;} } } return 0; }
int main() { freopen("Text/FINDPRM.txt","r",stdin); sieve(); int cases,n; VI::iterator it1,it2; scanf("%d",&cases); while(cases--) { scanf("%d",&n); it1=upper_bound(primes.begin(),primes.end(),n); it2=lower_bound(primes.begin(),primes.end(),(n+2)/2); printf("%d\n",it1-it2); } return 0; }
int main(){ //std::ios_base::sync_with_stdio(0); //FI; VI num; int l, i, n; while(si(n) != EOF){ num.pb(n); sort(num.begin(), num.end()); if(num.size() % 2 == 0){ pi((num[num.size() / 2] + num[num.size() / 2 - 1]) / 2); NL; } else{ pi(num[num.size() / 2]); NL; } } return 0; }
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 MAIN() { VI v; int ti; for(int i=0; i<9; ++i) { scanf("%d",&ti); //printf("%d\n",ti); v.PB(ti); } int n=encode(v); if(!n) { printf("\n"); } else { solve(n); VI v; while(n) { v.PB(arr[n]); n=prevv[n]; } reverse(v.begin(),v.end()); for(int i=0; i<v.size()-1; ++i) { printf("%d ",v[i]); } printf("%d\n",v[v.size()-1]); } }
int main() { //freopen("in", "r", stdin); int n; while(cin >> n) { VI vec; REP(i, n) { int x; scanf("%d", &x); int id = lower_bound(vec.begin(), vec.end(), x) - vec.begin(); if(id == vec.size()) vec.pb(x); else { vec[id] = x; } } cout << n - vec.size() << endl; }
void show() { VI::iterator it; int i=0; for(it=pos.begin();it!=pos.end();it++) cout<<"pos["<<i++<<"]=="<<*it<<' '; cout<<endl; }
string isPossible(vector<int> V) { VI xs; REP(i,SZ(V)-1) xs.push_back(V[i+1]/V[i]); sort(xs.begin(),xs.end()); REP(i,SZ(xs)) { int cnt=i+1; if(xs[i]<=cnt) return no; }
BoyerMoore(string pattern, int R = 256):_R(R),_pattern(pattern){ _right.resize(R); for(VI::iterator i = _right.begin(); i < _right.end(); i++){ *i = -1; } for(unsigned int j = 0; j < _pattern.size(); j++){ _right[(int)(pattern[j])] = j; } }
VI overlap(VI a, VI b) { sort(a.begin(), a.end()); sort(b.begin(), b.end()); VI c; for (int i = 0, j = 0; i < SIZE(a) && j < SIZE(b); ) if (a[i] == b[j]) { c.push_back(a[i]); i++; j++; } else if (a[i] < b[j]) i++; else j++; return c; }
int main(int argc, char *argv[]) { int d; cin>>d; valid(d,d,0,0); cout<<v.size()<<endl; set <int> s(v.begin(),v.end()); cout<<(s.size()==v.size())<<endl; return 0; }
VI find(int T, VI requiredTime) { sort(requiredTime.begin(), requiredTime.end()); int past = 0; int solved = 0; int penalty = 0; VI::const_iterator it; for (it = requiredTime.begin(); it != requiredTime.end(); ++it) { past += *it; if (past > T) { break; } ++solved; penalty += past; } VI r; r.push_back(solved); r.push_back(penalty); return r; }
void solve() { prevv[0]=0; if(max_flow()<k) { printf("Impossible\n"); return; } //assert(e>=MAXM); int ti; int f=0; for(int i=tst;i<tst+ct;++i) { ans.clear(); ti=prevv[i]; while(ti!=-1) { ans.PB(ti); if(ti==prevv[ti] || !ti) { break; } ti=prevv[ti]; } if(ans.size()==0) { continue; } reverse(ans.begin(),ans.end()); if(ans[0]==0) { ++f; printf("1 "); for(int j=2;j<ans.size();j+=2) { printf("%d ",(ans[j]-MAXK)/2); } printf("2\n"); if(f==k) { break; } } } }
int main() { int T; scanf("%d",&T); for(int kase = 1;kase <= T;kase++) { numy.clear(); numz.clear(); scanf("%d",&n); for(int i = 0;i < n;i++) { getPoint(x1[i],y1[i],z1[i]); getPoint(x2[i],y2[i],z2[i]); numz.PB(z1[i]); numz.PB(z2[i]); numy.PB(y1[i]); numy.PB(y2[i]); } sort(numz.begin(),numz.end()); sort(numy.begin(),numy.end()); numz.erase(unique(numz.begin(),numz.end()),numz.end()); numy.erase(unique(numy.begin(),numy.end()),numy.end()); printf("Case %d: ",kase); cout << solve() << endl; } return 0; }
int GetMaxFlow(int s, int t) { N = cap.size(); flow = VVI(N, VI(N)); reached = VI(N); int totflow = 0; while (int amt = Augment(s, t, INF)) { totflow += amt; fill(reached.begin(), reached.end(), 0); } return totflow; }
int nextGreaterElement(int n) { VI digits = getDigits(n); reverse(digits.begin(), digits.end()); nextPermutation(digits); int result = getNumber(digits); if (result <= n) { return -1; } else { return result; } }
int main(void){ int x; VI ans; while(cin >> x) ans.push_back(x); SORT(ans); reverse(ans.begin(), ans.end()); REP(i,ans.size()-1) cout << ans[i] << " "; cout << ans[ans.size()-1] << endl; return 0; }
void smooth_path(Network &nw,VI &path,double cutoff){ // remove points from path which are too close to each other int i=1; int last=0; while (i<(int)path.size()){ if (norm(nw.nodes[path[i]]-nw.nodes[path[last]])<cutoff){ path.erase(path.begin()+i); } else { last=i; i++; } } }