void path(int st){ Q.clear(); for(int i=0;i<=n;i++) fa[i] = i, inQ[i] = typ[i] = frm[i] = 0; typ[st] = inQ[st] = 1; Q.pb(st); while(sz(Q)){ int u = Q.front(); Q.pop_front(); inQ[u] = 0; for(int i=0;i<sz(E[u]);i++){ int v = E[u][i]; if(v != wife[u] && rt(u)!=rt(v) && typ[v]!=2){ if(typ[v]==1) contract(u, v); else if(!wife[v]){ frm[v] = u; for(int tv = v, tu = frm[tv], chW; tu && tv; tv = chW, tu = frm[tv]){ chW = wife[tu]; wife[tu] = tv, wife[tv] = tu; wife[chW] = 0; } return ; }else{ frm[v] = u, typ[v] = 2; inQ[wife[v]] = typ[wife[v]] = 1; Q.pb(wife[v]); } } } } }
int main (int argc, char const *argv[]) { while ( scanf("%d", &N) != EOF && N ) { while ( scanf("%d", &a) != EOF && a ) { S.clear(); v.clear(); S.pb(a); for (int i = 1; i < N; i += 1) { scanf("%d", &a); S.pb(a); } cnt = 0; for (int i = 1; i <= N; i += 1) { v.push_back(i); while ( !v.empty() && v.back() == S[cnt] ) { v.pop_back(); cnt++; } } if (v.empty()) { printf("Yes\n"); } else printf("No\n"); } printf("\n"); } return 0; }
int main() { freopen("C.INP","r",stdin); freopen("C.OUT","w",stdout); int tcs; scanf(" %d ",&tcs); for(int t = 1 ; t <= tcs ; t++) { dq.clear(); printf("Case %d:\n",t); int n; scanf(" %d ",&n); int a; scanf(" %d ",&a); dq.pb(a); puts("0"); for(int i = 1 ; i < n ; i++) { scanf(" %d ",&a); if(a >= dq.back()) { printf("%d\n",dq.size()); dq.pb(a); } else if(a <= dq.front()) { int cnt = 0; while(dq[cnt++] == a); printf("%d\n",cnt-1); dq.pf(a); } else { int pos = bSearch(a); if(dq[pos] == a) { while(dq[pos] == a) pos++; } else if(dq[pos] < a) pos++; printf("%d\n",pos); dq.insert(dq.begin()+pos,a); } //for(int i = 0 ; i < dq.size() ; i++) // printf("%d ",dq[i]); // printf("\n"); } } return 0; }
int main(int argc, char *argv[]) { setIO("tower"); n = gi; for(int i = 1;i<=n;++i) a[i] = gi; for(int i = 1;i<=n+1;++i) a[i] = a[i-1]+a[i]; q.push_front(n+1); for(int i = n;i>=0;--i) { while(!q.empty()) { int x = q.front();q.pof(); if(q.empty()){ q.pf(x); break; }else{ int y = q.front();q.pof(); if(a[x] >= a[y] && a[i]<=a[y]-f[y]) q.pf(y); else { q.pf(y),q.pf(x); break; } } } f[i] = a[q.front()]-a[i]; h[i] = h[q.front()]+1; ans = max(ans,h[i]); while(!q.empty()){ int x = q.back();q.pob(); if(a[x]-f[x] < a[i]-f[i] && a[x]>=a[i] ) continue; else{ q.pb(x); break; } } q.pb(i); } printf("%d\n",ans-1); closeIO(); return EXIT_SUCCESS; }
void contractPath(int u, int anc){ for(int wf, fm; u != anc; u = fm){ wf = wife[u], fm = frm[wf]; if(rt(fm) != anc) frm[fm] = wf; if(typ[wf]==2) Q.pb(wf), typ[wf] = inQ[wf] = 1; if(typ[fm]==2) Q.pb(fm), typ[fm] = inQ[fm] = 1; unionRt(u, wf), unionRt(wf, fm); } }
int main(){ int n,k,i; in >> n >> k; for(i = 1 ; i <= n ;++ i) in >> v[i]; for(i = 1 ; i <= k ;++ i){ rm_back(); Q.pb(i); } S = v[Q.front()]; for(; i <= n ; ++ i){ rm_back(); rm_front(); Q.pb(i); S += v[Q.front()]; } out << S << "\n"; }
void bfs(int source) { memset(viz, 0, sizeof(viz)); viz[source] = 1; q.pb(source); while (!q.empty()) { int node = q.front(); q.pop_front(); for (auto it : gr[node]) if (!viz[it]) { d[source][it] = d[source][node] + 1; viz[it] = 1; q.pb(it); } } }
int main() { char s[30], of[5]; int am; scanf("%d", &m); for(int i = 0; i < m; i++) { scanf("%d %s %s", &am, of, s); mp[string(s)] = i + 1; pro[i+1] = am; } scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d %s %s", &am, of, s); int ind = mp[string(s)]; line.pb(husb(ind, am, 0)); } line.pb(husb(-1, 0, 0)); while(42) { husb cur = line.front(); line.pop_front(); if(cur.i == -1) break; ans++; if(!pro[cur.i]) continue; if(cur.v <= pro[cur.i]) pro[cur.i] -= cur.v; else if((!cur.w && cur.v > pro[cur.i]) || (cur.w && cur.w != pro[cur.i])) { cur.w = pro[cur.i]; if(line.size()) { husb aux = cur; cur = *(line.begin()); *(line.begin()) = aux; } line.push_front(cur); } else if(cur.w && cur.w == pro[cur.i]) pro[cur.i] = 0; } printf("%d\n", ans); return 0; }
int main() { #ifndef ONLINE_JUDGE freopen("vd.inp","r",stdin); freopen("vd.out","w",stdout); #endif ios_base::sync_with_stdio(0); cin>>n>>m>>d; cin>>a>>b>>t; ll i,j; for(i=1;i<=n;++i) f[i]=b-abs(i-a); for(i=2;i<=m;++i) { cin>>a>>b>>t2; d2=d*(t2-t);t=t2; q.clear(); for(j=1;j<=n;++j) { while (q.size() && q.front()<j-2*d2) q.pop_front(); while (q.size() && f[q.back()]<=f[j]) q.pop_back(); q.pb(j); g[j]=f[q.front()]; } fm[n]=f[n]; for(j=n-1;j>=1;--j) fm[j]=max(fm[j+1],f[j]); for(j=1;j<=n;++j) { if (j+d2<=n) { f[j]=b-abs(a-j)+g[j+d2]; } else { if (j-d2>0) f[j]=b-abs(a-j)+fm[j-d2]; else f[j]=b-abs(a-j)+fm[1]; } } } j=1; for(i=1;i<=n;++i) if (f[i]>f[j]) j=i; cout<<f[j]; }
int maximizeResistance(string st, vector <int> cond) { sort( cond.bg , cond.en); for(int i=0;i<cond.sz;i++) in.pb( cond[i] ); print( in.sz); int ans=0; int n=st.sz; for(int i=n-1; i>0;) { int cn=0; char op='x'; cout<<"Enterung an loop with i \n"; print(i); bool cool=true; while( cool) { if( (st[i]=='A') || (st[i]=='B') ) { op=st[i]; i--; cool=false; break; } else { cn++; i--; } } print(cn); if( op=='A')//sum { for(int k=0;k<cn;k++) { ans+=in.back(); in.pop_back(); } } else { int mx=0; for(int k=0;k<cn;k++) { mx=max(mx,in.front() ); in.pop_front(); } ans=max(ans,mx); } } return ans; return -1; }