int sol(){ for (int i=1;i<=n;i++) d[i]=i, S.insert(w[i]); int a,b; while (m--) { scanf("%d%d",&a,&b); a=dsu(a); b=dsu(b); if (a!=b) { S.erase(S.find(w[a])); S.erase(S.find(w[b])); w[a]+=w[b]; w[b]=0; S.insert(w[a]); d[b]=a; } for (int i=1;i<=n;i++) cout<<d[i]<<" "; printf("%d\n",*S.begin()); } return 0; }
int main() { file_r("laundro_matt.txt"); file_w("out.out"); int cas = 0; cin >> T; while (T--) { cin >> l >> n >> m >> d; s.clear(); rep (i, n) { cin >> w[i]; s.insert(mp(w[i], w[i])); } q.clear(); m = min(l, m); rep (i, m) q.insert(0); ans = 0; rep (i, l) { iter = s.begin(); a[i] = iter->X; s.erase(iter); s.insert(mp(iter->X + iter->Y, iter->Y)); it = q.begin(); q.erase(it); if (*it <= a[i]) { ans = max(ans, a[i] + d); q.insert(a[i] + d); } else { ans = max(ans, *it + d); q.insert(*it + d); } }
int main() { int n,d,r; while(scanf("%d%d%d",&n,&d,&r)!=EOF&&n||d||r) { int k; for(int i=1;i<=n;i++) { scanf("%d",&k); s1.insert(k); } for(int i=1;i<=n;i++) { scanf("%d",&k); s2.insert(k); } int ans=0; for(int i=1;i<=n;i++) { int num=*s1.begin(); int index=d-num; __typeof(s2.begin()) it=s2.lower_bound(index); if(it==s2.end())it--; ans+=r*max(0,num+*it-d); s1.erase(s1.begin()); s2.erase(it); } printf("%d\n",ans); } return 0; }
int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%I64d",&sum[i]); num[i]=sum[i]; sum[i]+=sum[i-1]; } sum[n+1]+=sum[n]; s.insert(0); s.insert(n+1); ms.insert(-(sum[n+1]-sum[0])); for(int i=0;i<n;i++) { int x; scanf("%d",&x); int r=*s.lower_bound(x); int l=*(--s.lower_bound(x)); s.insert(x); ms.erase(ms.find(-(sum[r-1]-sum[l]))); ms.insert(-(sum[r-1]-sum[x])); ms.insert(-(sum[x-1]-sum[l])); printf("%I64d\n",-(*ms.begin())); } return 0; }
int main() { int n, q; scanf("%d %d", &n, &q); for (int i = 0; i < n; ++i) { scanf("%d", a+i); b[i] = a[i]; } sort(a, a+n); for (int i = 0; i < n; ++i) { val.insert(a[i]); if (i) dif.insert(a[i] - a[i-1]); } while (q--) { int t; scanf("%d", &t); if (t == 2) { long long ans = *val.rbegin() - *val.begin(); ans -= *dif.begin(); printf("%lld\n", ans); } else { int x, y; scanf("%d %d", &x, &y); --x; if (b[x] != y) { del(b[x]); add(b[x] = y); } } } return 0; }
void solve(){ for( int i = 1 ; i <= min( n , m ) ; i ++ ){ done[ i ] = ti[ i ]; S.insert( ti[ i ] ); } for( int i = m + 1 ; i <= n ; i ++ ){ done[ i ] = *S.begin() + ti[ i ]; S.erase( S.find( *S.begin() ) ); S.insert( done[ i ] ); } for( int i = 1 ; i <= n ; i ++ ) pmax[ i ] = max( pmax[ i - 1 ] , done[ i ] ); D ans = good[ n ] * pmax[ n ]; for( int i = 1 ; i <= n ; i ++ ) ans += ( good[ i - 1 ] * ( 1.0 - p[ i ] ) ) * pmax[ i ]; // D ans = pmax[ n ]; // for( int i = 1 ; i <= n ; i ++ ) // if( ans > eps ) // ans *= p[ i ]; // else break; // for( int i = 1 ; i <= n ; i ++ ){ // D tans = pmax[ i ] * ( 1.0 - p[ i ] ); // for( int j = 1 ; j < i ; j ++ ) // if( tans > eps ) // tans *= p[ j ]; // else break; // ans += tans; // } printf( "%.12f\n" , (double)ans ); }
int main(int argc, char const *argv[]) { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%I64d", A + i); s += A[i]; ps[i + 1] = ps[i] + A[i]; } sum.insert(s); qu.insert(Pii(n, 0)); for (int i = 0, p; i < n; ++i) { scanf("%d", &p); --p; auto it = qu.upper_bound(Pii(p, 0)); // cout << it->second << " " << it->first << endl; ll tmp = ps[it->first] - ps[it->second]; // cout << "tmp " << tmp << endl; sum.erase(sum.lower_bound(tmp)); tmp = ps[p] - ps[it->second]; // cout << "tmp " << tmp << endl; sum.insert(tmp); qu.insert(Pii(p, it->second)); tmp = ps[it->first] - ps[p + 1]; // cout << "tmp " << tmp << endl; sum.insert(tmp); qu.insert(Pii(it->first, p + 1)); qu.erase(it); printf("%I64d\n", *sum.rbegin()); } return 0; }
void insert(int num) { if(median == -1) median = num; else { if(num < median) small_number_set.insert(num); else big_number_set.insert(num); if(small_number_set.size() + 1 < big_number_set.size()) { small_number_set.insert(median); auto median_iter = big_number_set.begin(); median = *median_iter; big_number_set.erase(median_iter); } else if(small_number_set.size() > big_number_set.size()) { big_number_set.insert(median); auto median_iter = prev(small_number_set.end()); median = *median_iter; small_number_set.erase(median_iter); } } }
int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> t; while(t--){ S.clear(); cin >> n; for(int i=0;i<n;i++){ cin >> x; it = S.upper_bound(x); if(it==S.end()){ S.insert(x); } else{ S.erase(it); S.insert(x); //(*it) = x; } } cout << S.size() << " "; for(multiset<int>::iterator it=S.begin();it!=S.end();it++){ cout << (*it) << " "; } cout << endl; } return 0; }
void try_to_add_or_delete(cl_term* Sp, cl_term* S) { string Sp_str = Sp->conv2str(); string S_str = S->conv2str(); Sp_nchecker.insert(Sp_str); S_nchecker.insert(S_str); if (Sp_nchecker.count(Sp_str) >= max_SS_n || S_nchecker.count(S_str) >= max_SS_n) { multiset_delete_one(Sp_nchecker, Sp_str); multiset_delete_one(S_nchecker, S_str); delete Sp; delete S; return; } if (uniqchecker.try_to_add(Sp_str, S_str)) { population.push_back(make_cl_ssprimega_member(Sp, S)); } else { multiset_delete_one(Sp_nchecker, Sp_str); multiset_delete_one(S_nchecker, S_str); delete Sp; delete S; return; } }
int main() { srand(time(NULL)); for(int i=6; i<21; ++i) { char fin[55],out[55]; sprintf(fin,"teste/grader_test%d.in",i); sprintf(out,"teste/grader_test%d.ok",i); ofstream in(fin); ofstream g(out); n=rand()%DN+1;k=rand()%n+1; in<<n<<' '<<k<<'\n'; for(int i=1; i<=n; ++i) { p[i]=rand()%DK+1; c[i]=rand()%DK+1; in<<p[i]<<' '<<c[i]<<'\n'; } pt=0; rst.clear(); sol.clear(); ssol=0; rez=0; for(int i=1; i<=k; ++i) { pt+=p[i]; rst.insert(p[i]+c[i]); } rez=max(rez,pt); for(int i=k+1; i<=n; ++i) { pt+=p[i]; rst.insert(p[i]+c[i]); ssol+=*rst.begin(); sol.insert(*rst.begin()); rst.erase(rst.begin()); bst[i]=pt-ssol; rez=max(rez,bst[i]); } g<<rez; } return 0; }
int main() { #ifndef ONLINE_JUDGE freopen("519b.in", "r", stdin); freopen("519b.out", "w", stdout); #endif cin >> n; for(int i = 1 ; i <= n ; ++ i) { int x; cin >> x; s1.insert(x); } for(int i = 1 ; i < n ; ++ i) { int x; cin >> x; s1.erase(s1.find(x)); s2.insert(x); } cout << *s1.begin() << '\n'; for(int i = 1 ; i < n - 1 ; ++ i) { int x; cin >> x; s2.erase(s2.find(x)); } cout << *s2.begin() << '\n'; }
int main() { int n, elm, k, t; cin >> n >> t >> k; for (int i = 0; i < n; ++i) { cin >> elm; v.push_back(elm); } for (int i = 0; i < k; ++i) { S.insert(v[i]); } it = S.rbegin(); ans.push_back(*it); for (int i = k; i < n; ++i) { S.erase(S.find(v[i - k])); S.insert(v[i]); it = S.rbegin(); ans.push_back(*it); } int res = 0; for (int i = 0; i < ans.size(); ++i) { if (ans[i] <= t) ++res; } cout << res; return 0; }
int main() { freopen("b.in", "r", stdin); while (~scanf("%d %lld", &n, &p)) { s.clear(); long long x; for (int i = 0; i < n; i++) { scanf("%lld", &x); arr[i] = x; s.insert(x); } long long ans = 0; for (int i = 0; i < n; i++) { long long now = arr[i]; set<long long>::iterator it = s.lower_bound(now); s.erase(it); it = s.lower_bound(p - now); if (it != s.begin()) it--; ans = max(ans, (now + *it) % p); it = s.lower_bound(p + p - now); if (it != s.begin()) it--; ans = max(ans, (now + *it) % p); s.insert(now); } cout << ans << endl; } return 0; }
int main () { scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d %d", x+i, y+i); p[i] = i; } int res = 0; sort(p, p+n, cmp_t); for (int i = 0; i < k; i++) s.insert(0); for (int i = 0; i < n; i++) { it = s.upper_bound(x[p[i]]); if (it == s.begin()) continue; --it; res++; s.erase(it); s.insert(y[p[i]]); } printf("%d\n", res); }
void update(int a, int b) { if (f[a] != -1) { int temp = *SS[f[a]][c[a]].begin(); SS[f[a]][c[a]].erase(SS[f[a]][c[a]].find(d[a])); if (SS[f[a]][c[a]].empty()) SS[f[a]].erase(c[a]); if (c[f[a]] != c[a]) { S.erase(S.find(temp)); if (SS[f[a]].find(c[a]) != SS[f[a]].end() && !SS[f[a]][c[a]].empty()) S.insert(*SS[f[a]][c[a]].begin()); } if (c[f[a]] != b) { if (!SS[f[a]][b].empty()) { if (d[a] < *SS[f[a]][b].begin()) { S.erase(S.find(*SS[f[a]][b].begin())); S.insert(d[a]); } } else { S.insert(d[a]); } } SS[f[a]][b].insert(d[a]); } if (SS[a].find(c[a]) != SS[a].end()) S.insert(*SS[a][c[a]].begin()); if (SS[a].find(b) != SS[a].end()) S.erase(S.find(*SS[a][b].begin())); c[a] = b; }
int main(){ ifstream fin("input.txt"); ofstream fout("output.txt"); int n, k; long long tmp, t; fin>>n>>k; for(int i=0; i<n; ++i){ fin>>tmp; if(q.size()==k){ t=*q.begin(); q.erase(q.begin()); q.insert(t+tmp); } else q.insert(tmp); } while(q.size()>1) q.erase(q.begin()); fout<<*q.begin()<<endl; fin.close(); fout.close(); return 0; }
int main() { int num; while(cin>>num&&num) { int key; vec.clear(); for(int i=0;i<num;i++) { cin>>key; vec.insert(key); } int sum=0; int x,y,s; while(vec.size()>=2) { multiset<int>::iterator it1=vec.begin(); x=*it1; vec.erase(it1); multiset<int>::iterator it2=vec.begin(); y=*it2; vec.erase(it2); sum=sum+x+y; vec.insert(x+y); } cout<<sum<<endl; } return 0; }
main() { int i,j,n,t,C; multiset<data>::iterator now,last; scanf("%d",&t); for(C=1;C<=t;C++) { scanf("%d",&n); s.clear(); s.insert((data){-1,1000000001}); s.insert((data){1000000001,-1}); if(C>1)puts(""); printf("Case #%d:\n",C); while(n--) { scanf("%d %d",&i,&j); while(1) { last=s.upper_bound((data){i,j}); if(j>(now=last--)->b) break; s.erase(*now); } if(j<last->b ||(i==last->a && j==last->b)) s.insert((data){i,j}); printf("%d\n",s.size()-2); } } }
main() { int n,m,Q ; scanf("%d%d%d",&m,&n,&Q) ; s1.insert(0) ; s1.insert(n) ; s2.insert(0) ; s2.insert(m) ; d1.insert(n) ; d2.insert(m) ; while(Q--) { char s[6] ; int x ; scanf("%s%d",s,&x) ; if(s[0]=='H') { auto it=s1.lower_bound(x) ; int x2=*it ; it-- ; int x1=*it ; d1.erase(d1.find(x2-x1)) ; d1.insert(x2-x) ; d1.insert(x-x1) ; s1.insert(x) ; } else { auto it=s2.lower_bound(x) ; int x2=*it ; it-- ; int x1=*it ; d2.erase(d2.find(x2-x1)) ; d2.insert(x2-x) ; d2.insert(x-x1) ; s2.insert(x) ; } printf("%I64d\n",(LL)(*d1.begin())*(*d2.begin())) ; } }
int main(int argc, char *argv[]) { std::ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; for(int i = 0; i < n; ++i) cin >> a[i]; S.insert(a[0]); int ma = 0, lo = 0, hi = 1; while(true) { if(*S.rbegin() - *S.begin() <= k) { if(hi - lo > ma) { ma = hi - lo; ans.clear(); } if(hi - lo == ma) ans.push_back(lo); if(hi == n) break; S.insert(a[hi++]); } else { S.erase(S.find(a[lo++])); } } cout << ma << ' ' << ans.size() << endl; for(int i = 0; i < (int)ans.size(); ++i) cout << ans[i] + 1 << ' ' << ans[i] + ma << endl; return 0; }
int main() { scanf("%d", &ile); czas = ile - 1; for(int i = 1; i <= ile; i++) { scanf("%d",&wcz1); wierz[i].czasRozp = wcz1; wierz[i].numer = i; } for(int i = 0; i + 1 < ile; i++) { scanf("%d%d", &wcz1, &wcz2); wierz[wcz1].lacznik.push_back(&wierz[wcz2]); wierz[wcz2].lacznik.push_back(&wierz[wcz1]); } dfs(1,0); for(int i = 1; i <= ile; i++) { wierz[i].suma = wierz[i].odl + wierz[i].czasRozp; czyByl[i] = false; } zbior.insert(wierz[1]); while(zbior.size() > 0) { Wierz nowy = *zbior.begin(); zbior.erase(zbior.begin()); czyByl[nowy.numer] = true; for(int i = 0; i < nowy.lacznik.size(); i++) { if(czyByl[nowy.lacznik[i]->numer] == false) { zbior.insert(*wierz[nowy.numer].lacznik[i]); } } wynik = max(wynik, nowy.suma + czas); czas--; } printf("%d",wynik); return 0; }
LL cal(int x) { cyc_p.clear() ; cyc_e.clear() ; dfs0(x) ; int num=cyc_p.size() ; for(int i=0;i<num;i++) used[cyc_e[i]]=1 , tree[i].clear() ; for(int i=0;i<num;i++) dfs1(cyc_p[i],i) ; /* for(int i=0;i<num;i++) {for(int j=0;j<tree[i].size();j++) printf("%d,",tree[i][j]) ; printf("\n") ; } */ LL ret=0LL ; for(int i=0;i<num;i++) len[i]=max_length1(cyc_p[i],i) ; for(int i=0;i<num;i++) ret=max(ret,max_length2(i)) ; /* for(int i=0;i<num;i++) printf("%d , %d , %I64d\n",cyc_p[i],edge[cyc_e[i]].dis,len[i]); */ mst.clear() ; LL tot=0LL ; for(int i=0;i<num;i++) mst.insert(tot+len[i]) , tot+=edge[cyc_e[i]].dis ; LL sum=0LL ; for(int i=0;i<num;i++) { mst.erase(mst.find(sum+len[i])) ; ret=max(ret,len[i]+(*(mst.begin()))-sum) ; mst.insert(tot+sum+len[i]) ; sum+=edge[cyc_e[i]].dis ; } //printf("ret=%I64d\n",ret) ; return ret ; }
int main() { freopen("ti.in","r",stdin); scanf("%d",&zu); for (zz=1;zz<=zu;zz++) { shu.clear(); scanf("%d%d",&n,&m); for (i=1;i<=n;i++) scanf("%d",&shi[i]); if (n<m) { cout<<0<<endl; continue; } for (i=1;i<=m;i++) shu.insert(shi[i]); for (i=m+1;i<=n;i++) { t=*(shu.begin()); shu.erase(shu.begin()); shu.insert(t+shi[i]); } t=*(shu.begin()); cout<<t<<endl; } return 0; }
int main() { static multiset<int> st; int n; while (scanf("%d", &n) != EOF) { st.clear(); for (int i = 0; i < n; i++) { scanf("%d", a + i); st.insert(a[i]); } static int tmp; int ans = INT_MAX; for (int i = 0; i < n; i++) { scanf("%d", &tmp); st.erase(st.find(a[i])); ans = min(ans, tmp + *st.begin()); st.insert(a[i]); } printf("%d\n", ans); } }
void solve() { int rez = 0; multiset <int> :: iterator it; for (int i = 1; i <= n; ++i) { it = s.lower_bound(v[i].first); if (it != s.end() && *it == v[i].first) { s.erase(it); s.insert(v[i].second); ++rez; } else { if (it == s.begin()) continue; --it; s.erase(it); s.insert(v[i].second); ++rez; } } printf("%d\n", rez); }
void insert(int num) { if (num >= median) right.insert(num); else left.insert(num); balance(); median = cal(); }
int main() { multiset<int>::iterator it; while (scanf("%d", &n) != EOF) { while (!S.empty()) S.pop(); lef.clear(); righ.clear(); for (int i = 0; i < n; ++i) { scanf("%s", buf); if (strcmp(buf, "Pop") == 0) { if (S.empty()) { printf("Invalid\n"); continue; } int u = S.top(); S.pop(); if (u <= med) { it = lef.find(u); lef.erase(it); } else { it = righ.find(u); righ.erase(it); } adjust(); printf("%d\n", u); } else if (strcmp(buf, "PeekMedian") == 0) { if (S.empty()) { printf("Invalid\n"); continue; } printf("%d\n", med); } else if (strcmp(buf, "Push") == 0) { int u; scanf("%d", &u); if (S.empty()) { med = u; lef.insert(u); } else if (u > med) { righ.insert(u); } else { lef.insert(u); } S.push(u); adjust(); } else printf("Invalid\n"); } } return 0; }
int main() { int idx, x; while(~scanf("%d%d", &n, &m)) { se.clear(); se2.clear(); mn = INF; for(int i=1; i<=n; i++) { scanf("%d", a+i); se.insert(a[i]); b[i] = a[i]; } for(int i=2; i<=n; i++) se2.insert(myabs(a[i]-a[i-1])); for(multiset<int>::iterator it = se.begin(); ; ) { multiset<int>::iterator it2 = it; it++; if(it == se.end()) break; mn = min(mn, myabs((*it) - (*it2))); } //show_se(); //cout << "Min: " << mn << endl; while(m--) { scanf("%s", com); if(com[0] == 'I') { scanf("%d%d", &idx, &x); multiset<int>::iterator it = se.lower_bound(x); mn = min(mn, myabs((*it) - x)); if(it!=se.begin()) { it--; mn = min(mn, myabs(x - (*it))); } se.insert(x); if(idx!=n) { it = se2.lower_bound(myabs(b[idx] - a[idx+1])); //se2.erase(myabs(b[i] - a[i+1])); se2.erase(it); se2.insert(myabs(x - a[idx+1])); } se2.insert(myabs(b[idx] - x)); b[idx] = x; //show_se(); } else if(com[1] == 'i') { printf("%d\n", mn); } else { multiset<int>::iterator it = se2.end(); it--; printf("%d\b", *it); } } } return 0; }
/* Function: PerformInsert(int value, * multiset<int>& minSet, * multiset<int>& maxSet) * Usage: bool success = PerformInsert(value, minSet, maxSet); * ----------------------------------------------------------------------------- * Inserts one instance of the specified value into the minset or the maxset, * according to the following logic: if the value exceeds the current maximum * value in the minset, then add the value to the maxset; otherwise, add the * value to the minset. The function returns true if the insert operation was * successful, and false otherwise. */ bool PerformInsert(int value, multiset<int>& minSet, multiset<int>& maxSet) { if (minSet.empty() || value <= *FindMaxElement(minSet)) minSet.insert(value); else maxSet.insert(value); /* Always return true. */ return true; }