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; }
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; }
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; }
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; }
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()); }
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; }
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; }
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]); } }
void show() { VI::iterator it; int i=0; for(it=pos.begin();it!=pos.end();it++) cout<<"pos["<<i++<<"]=="<<*it<<' '; cout<<endl; }
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; }
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; }
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 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; }
int GetMaxFlow(int source, int sink) { fill(Q.begin(), Q.end(), 0); for (int i = 0; i < N; ++i) { fill(flow[i].begin(), flow[i].end(), 0); } int totflow = 0; while (int flow = BlockingFlow(source, sink)) totflow += flow; return totflow; }
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()); }
int main() { si(N); si(M); while(M--){ int x, y; si(x); x--; si(y); y--; if(x!=y){ G[x].insert(y); G[y].insert(x); } } si(s); si(t); s--; t--; dfs(s,1); sort(TP.begin(),TP.end()); printf("%d\n",TP.size()); for(auto x : TP) printf("%d\n",x); return 0; }
int get_ans(VI have, VI need) { if (need.size() == 0) return 0; int tot = 0; for (int i = 0 ; i < have.size() ; ++i) tot += have[i]; for (int i = 0 ; i < need.size() ; ++i) tot -= need[i]; if (tot < 0) return INF; sort(have.begin(), have.end()); sort(need.begin(), need.end()); int dp[16][52]; memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; for (int i = 0 ; i <= need.size() ; ++i) { for (int j = 0 ; j <= have.size() ; ++j) { if (i == 0 && j == 0) continue; if (j) dp[i][j] = min(dp[i][j], dp[i][j-1]); if (i) dp[i][j] = min(dp[i][j], dp[i-1][j] + need[i-1]); if (i && j) dp[i][j] = min(dp[i][j], dp[i-1][j-1] + (need[i-1] <= have[j-1] ? 0 : need[i-1] - have[j-1])); } } return dp[need.size()][have.size()]; }
int DoInference(const VVVVI &phi, const VVI &psi, VI &x) { int M = phi.size(); cap = VVI(M+2, VI(M+2)); VI b(M); int c = 0; for (int i = 0; i < M; i++) { b[i] += psi[i][1] - psi[i][0]; c += psi[i][0]; for (int j = 0; j < i; j++) b[i] += phi[i][j][1][1] - phi[i][j][0][1]; for (int j = i+1; j < M; j++) { cap[i][j] = phi[i][j][0][1] + phi[i][j][1][0] - phi[i][j][0][0] - phi[i][j][1][1]; b[i] += phi[i][j][1][0] - phi[i][j][0][0]; c += phi[i][j][0][0]; } } #ifdef MAXIMIZATION for (int i = 0; i < M; i++) { for (int j = i+1; j < M; j++) cap[i][j] *= -1; b[i] *= -1; } c *= -1; #endif for (int i = 0; i < M; i++) { if (b[i] >= 0) { cap[M][i] = b[i]; } else { cap[i][M+1] = -b[i]; c += b[i]; } } int score = GetMaxFlow(M, M+1); fill(reached.begin(), reached.end(), 0); Augment(M, M+1, INF); x = VI(M); for (int i = 0; i < M; i++) x[i] = reached[i] ? 0 : 1; score += c; #ifdef MAXIMIZATION score *= -1; #endif return score; }
void print(int mask) { int last = bestL[mask]; VI TP; while(last>0) { push_sp(P[mask][last] , last, TP); int nmask = mask - (1<<last); last = P[mask][last]; mask = nmask; } TP.PB(1); reverse(TP.begin(),TP.end()); printf("%d",TP.size()-1); for(auto c : TP)printf(" %d",c); printf("\n"); }
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; }