bool walk1(VI &v, VI &w, int step, int k/*,set<VI> sol*/) { if (DBG) cerr << step << " " << k << " " << v.size() << endl; if (k == 0) return true; // TODO change that if (step == (int) v.size()) { set<PII> in; return walk2(v, w, 0, 1, in, set<PII>(), k/*,sol*/); } if (walk1(v, w, step + 1, k/*,sol*/)) return true; int c = 2; int mem = v[step]; while (c <= v[step]) { v[step] -= c; w[step] = c; k -= 1; ++c; if (walk1(v, w, step + 1, k/*,sol*/)) return true; } v[step] = mem; w[step] = 1; return false; }
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]); } }
bool check(VI& cur) { if (cur.size() < 2) return false; for (int i = 1; i < cur.size(); ++i) { if (cur[i] < cur[i - 1]) return false; } return true; }
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 getNumber(vector<int> c, int X) { sort(all(c)); int ans=1e9; VI a; for(int i=0;i<c.size();++i) { a.pb(c[i]+X); a.pb(c[i]-X); } sort(all(a)); for(int i=0;i<a.size();++i) { for(int j=i;j<a.size();++j) { bool ok=true; for(int k=0;k<c.size();++k) { ok=ok&&(f(c[k]+X,a[i],a[j]) || f(c[k]-X,a[i],a[j])); } if(ok)ans=min(ans,a[j]-a[i]); } } return ans; }
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; }
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; }
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; }
int main() { #ifdef MANGOGAO freopen("data.in", "r", stdin); #endif int n, m, x; scanf("%d%d", &n, &m); int now, pre; dp[0][0][0] = 1; for (int i = 1; i <= n; i++) { scanf("%d", &x); now = i & 1; pre = 1 - now; for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) { dp[now][j][k] |= dp[pre][j][k]; if (j >= x) { dp[now][j][k] |= dp[pre][j - x][k]; if (k >= x) dp[now][j][k] |= dp[pre][j - x][k - x]; } } } VI ans; for (int i = 0; i <= m; i++) if (dp[now][m][i]) ans.pb(i); printf("%d\n", ans.size()); for (int i = 0; i < (int)ans.size(); i++) printf("%d%c", ans[i], i == (int)ans.size() - 1 ? '\n' : ' '); return 0; }
bool lex_smaller(const VI &v1, const VI &v2){ assert(v1.size() == v2.size()); for(int i = v1.size()-1; i >= 0; i--){ if(v1[i] != v2[i]){ return v1[i] < v2[i]; } } return false; }
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; }
int main(){ int n,rlast,clast; si(n); for(int i=0;i<n;i++){ si(a[i].F.F);si(a[i].F.S); a[i].S=i+1; if(i==n-1){ clast=a[i].F.F; rlast=a[i].F.S; } } sort(a,a+n); maxx[0].F=a[i].F.S,maxx[0].S=a[i].S; for(int i=1;i<n;i++){ if(maxx[i-1]<a[i].F.S){ maxx[i].F=a[i].F.S; maxx[i].S=a[i].S; } else{ maxx[i].F=maxx[i-1].F; maxx[i].S=maxx[i-1].S; } } VI v; int c=0,r=1; for(int i=1;i<n;i++){ if(clast<=r){ if((int)v.size()==0){ cout << 1 << endl; cout << n << endl; } else{ cout << (int)v.size()+1 << endl; for(int i=0;i<SZ(v);i++){ cout << v[i] << ' '; } cout << n << endl; } return 0; } if(a[i+1].F.F<=r) continue; else if(a[i+1].F.F>r){ c=a[i].F.F; r=maxx[i].F; v.PB(maxx[i].S); if(a[i+1].F.F<=r) continue; else{ cout << "No such luck" << endl; return 0; } } } return 0; }
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; } } } }
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 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; }
VI heapSort(VI &v){ for(int i=(v.size()-1)/2;i>=0;i--){ fixHeap(v,i,v.size()); } int size = v.size(); for(int i=0;i<v.size();i++){ int tmp = v[0]; v[0] = v[size-1]; v[size-1]=tmp; fixHeap(v,0,size-1); size--; } return v; }
int main() { VI v; MI m; int x = m.size(); size_t y = v.size(); m.size(); f(); string s = "12312321"; x = s.empty(); int z = v.size(); v[0] = 1; s[0] = 'a'; m[0][0] = 1; }
int removeThem(vector<int> a, int K) { k=K; n=a.size(); a0.clear(); a1.clear(); a2.clear(); a3.clear(); for(int i=0;i<a.size();++i) { if(a[i]%4==0)a0.pb(a[i]); if(a[i]%4==1)a1.pb(a[i]); if(a[i]%4==2)a2.pb(a[i]); if(a[i]%4==3)a3.pb(a[i]); } sort(a0.rbegin(),a0.rend()); sort(a1.rbegin(),a1.rend()); sort(a2.rbegin(),a2.rend()); sort(a3.rbegin(),a3.rend()); B[0][0]=a0[0]; for(int i=1;i<a0.size();++i) { B[0][i]=B[0][i-1]+a0[i]; } B[1][0]=a1[0]; for(int i=1;i<a1.size();++i) { B[1][i]=B[1][i-1]+a1[i]; } B[2][0]=a2[0]; for(int i=1;i<a2.size();++i) { B[2][i]=B[2][i-1]+a2[i]; } B[3][0]=a3[0]; for(int i=1;i<a3.size();++i) { B[3][i]=B[3][i-1]+a3[i]; } fill(dp,-1); return f(0,0,0,0); }
int main() { freopen("Text/EUROPEAN.txt","r",stdin); int cases; int ti; VI ans; scanf("%d",&cases); int i; for(int t=1;t<=cases;++t) { ct=0; MSET(fl,0); fs=111; scanf("%d",&n); for(i=0;i<n;++i) { scanf("%d",&ti); tarr[i]=ti; } ans.clear(); finn.clear(); ans.PB(0); sort(tarr,tarr+n); solve(ans); printf("Scenario #%d\n",t); printf("%d:",finn.size()); for(i=0;i<finn.size();++i) { printf(" %d",finn[i]); } printf("\n\n"); } return 0; }
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; }
SEGTREE(const VI & vi) { n = vi.size(); tree = VI(4*n, 0); lazy = VI(4*n, 0); init(1, 0, n-1, vi); // construct SegmentTree }
void CalcH(const VVI OmegaM, const VI LM, VVD & H) { if (OmegaM.size() == 0 || LM.size() == 0) { cerr << "Size of Omega matrix or Lambda matrix is 0 !" << endl; exit(-1); } H.clear(); VD vdTmp(OmegaM[0].size(), 0); H.insert(H.begin(), OmegaM.size(), vdTmp); unsigned int i, j; for (i = 0; i < H.size(); ++i) for (j = 0; j < H[0].size(); ++j) { H[i][j] = 1.0 * OmegaM[i][j] / LM[i]; } /* for(it=DMap.begin(), i=0; it!= DMap.end(); ++it, ++i) { for(j=0;j<RM.size();++j) { sum = 0; for(k=0;k<(*it).second.size();++k) { sum += RM[j][(*it).second[k]]; } H[i][j] = 1.0 * sum / LM [j]; } }*/ }
int run(int goodValue, VI d) { D = d; N = d.size(); // 1の場合は、全て使わない場合が含まれているので、それをのぞく return dfs(goodValue, 0) + (goodValue == 1 ? -1 : 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 period(const VI& v, int A, int L, int n, LL& count) { if (v.size() == n) { count++; return; } VI t = v; t.pb(0); int At = 0; int Lt = L; period(t, At, Lt, n, count); if (A != 2) { t = v; t.pb(1); At = A+1; Lt = L; period(t, At, Lt, n, count); } if (L == 0) { t = v; t.pb(2); At = 0; Lt = 1; period(t, At, Lt, n, count); } }
vector<string> solve(VI w) { int N = w.size(); int all = accumulate(ALL(w), 0LL); vector<string> ans; while(all) { // DD(w); vector<PII> ww; REP(i, N) ww.PB(MP(w[i], i)); sort(ALL(ww)); reverse(ALL(ww)); int no = 2; if(ww.size()<2) no=1; if(no==2 && ww[1].first==0) no=1; string lans; if(no==2 && ww[0].first > ww[1].first+1) { lans = string(2, 'A'+ww[0].second); w[ww[0].second]-=2; all-=2; } else { VI tw(w); REP(i, no) { tw[ww[i].second]--; } bool ok = check(tw); if(!ok) no=1; REP(i, no) { lans.PB('A'+ww[i].second); w[ww[i].second]--; all--; } } ans.PB(lans); // cout<<lans<<endl; check(w, true); }
void run(int u, int l, VI p, vector<VI> used){ vector<VI> v; VI np; if(l==8){ for(int i=0; i<p.size(); i++){ printf("%d",p[i]+1); } printf("\n"); } else{ for(int j=0; j<5; j++){ if(m[u][j]){ if(!used[u][j]){ v.assign(5,VI()); np = p; np.push_back(j); for(int i=0; i<5; i++) for(int j=0; j<5; j++) v[i].push_back(used[i][j]); v[u][j] = 1; v[j][u] = 1; run(j,l+1,np,v); } } } } }
double doExec() { if (!loadTestData()) { std::cerr << "Failed to load test data" << std::endl; return -1; } // // Start solution testing // double score = 0, sse; int scenario = 0; for (int i = 0; i < subsetsNum; i++) { scenario = i % 3; generateTestData(scenario, i); VD res = test->predict(0, scenario, DTrain, DTest); Assert(groundTruth.size() == res.size(), "Expected results count not equal to found. Expected: %lu, but found: %lu", groundTruth.size(), res.size()); sse = 0; for (int j = 0; j < res.size(); j++) { double e = res[j] - groundTruth[j]; sse += e * e; } // calculate score double s = 1000000 * fmax(0, 1.0 - sse/sse0); Printf("%i.) Score = %f, sse: %f, sse0: %f\n", i, s, sse, sse0); score += s; } return score / subsetsNum; }
int main() { VVI graph(9,VI(9,0)); // Create a graph with 9 nodes and no edges (empty adjacency matrix) // Add the undirected, weighted edges graph[0][1] = graph[1][0] = 3; graph[0][3] = graph[3][0] = 7; graph[1][2] = graph[2][1] = 1; graph[2][4] = graph[4][2] = 9; graph[3][2] = graph[2][3] = 3; graph[3][4] = graph[4][3] = 6; graph[3][6] = graph[6][3] = 11; graph[5][4] = graph[4][5] = 2; graph[6][5] = graph[5][6] = 4; graph[6][8] = graph[8][6] = 5; graph[7][3] = graph[3][7] = 5; graph[7][8] = graph[8][7] = 4; graph[8][5] = graph[5][8] = 8; // Do the Dijkstra algorithm forthe first node VI distances = dijkstra(graph,0); // Output should be 0 3 4 7 13 15 18 12 16 for (int i = 0; i < distances.size(); ++i) { std::cout << distances[i] << " "; } std::cout << std::endl; }
int run(VI position, VI count, int time) { int N = position.size(); vector<pair<int, int> > V; REP(i, N) V.push_back(make_pair(position[i], count[i])); sort(ALL(V)); int current = -1 << 31; int counter = 0; int flag = false; // true if you can put one in the same position (1 of C) REP(i, N){ int p = V[i].first; int n = V[i].second; REP(k, n){ // 左端の場合 int next; if (current + 1 <= p - time){ flag = false; next = p - time; } else { if (flag) break; next = current + 1; } // 右端の場合 if (current >= p + time){ current = p + time; flag = true; counter++; break; } current = next; }