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() { #ifdef fn freopen(fn".in", "r", stdin); freopen(fn".out", "w", stdout); #endif scanf("%d", &n); int nn = n * n; for (int i = 1; i <= nn; i++) { scanf(I64, g + i); s.insert(-g[i]); } for (int i = 1; i <= n; i++) { a[i] = -(*s.begin()); //printf("%d ", a[i]); s.erase(s.begin()); for (int j = 1; j < i; j++) { s.erase(s.find(-__gcd(a[j], a[i]))); s.erase(s.find(-__gcd(a[j], a[i]))); } } for (int i = 1; i <= n; i++) printf(I64 " ", a[i]); }
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; }
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(){ ll n, res, k, val; while(true){ scanf("%lld", &n); if(n == 0) break; s.clear(); res = 0; while(n--){ scanf("%lld", &k); for(int j = 0; j < k; j++){ scanf("%lld", &val); s.insert(val); } res += *s.begin() - *prev(s.end()); s.erase(s.begin()); s.erase(prev(s.end())); } printf("%lld\n", res); } 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'; }
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; }
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 n; while (scanf("%d", &n) != EOF) { if (n==0) break; LL ans = 0; num.clear(); for (int i = 0; i < n; i++) { int k; scanf("%d", &k); for (int j = 0; j < k; j++) { int x; scanf("%d", &x); num.insert(x); } ans += *num.rbegin() - *num.begin(); num.erase(num.find(*num.begin())); num.erase(num.find(*num.rbegin())); } printf("%I64d\n", ans); } return 0; }
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; }
int main() { int n, m, a; while ( scanf("%d", &n), n ) { long long ans = 0LL; urn.clear(); for (int i = 0; i < n; i++) { scanf("%d", &m); for (int j = 0; j < m; j++) { scanf("%d", &a); urn.insert( a ); } ans += *urn.rbegin() - *urn.begin(); urn.erase( urn.begin() ); urn.erase( urn.find(*urn.rbegin()) ); } printf("%lld\n", ans); } return 0; }
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 remove(int num) { if (num >= median) right.erase(right.find(num)); else left.erase(left.find(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() { //freopen("I.in","r",stdin); //freopen("I.out","w",stdout); int T; scanf("%d", &T); for(int cas=1;cas<=T;cas++) { mst.clear(); int n, m, k, cnt = 0; long long num; scanf("%d%d%d", &n, &m, &k); for(int i = 0; i < n; ++i) { scanf("%lld", &num); if(num <= k) { mst.insert(num); } } while(true) { int sz = mst.size(); if(sz < 2) break; it1 = mst.end(); it1--; long long fst = *it1; mst.erase(it1); it2 = mst.lower_bound(min(fst, k - fst)); if(it2 == mst.end()) it2--; if(it2 == mst.begin() && *it2 > (k - fst)) continue; if(*it2 <= k - fst) { ans[cnt++] = fst * ( *it2); mst.erase(it2); } else { it2 --; ans[cnt++] = fst * (*it2); mst.erase(it2); } } sort(ans, ans + cnt, cmp); long long res = 0; for(int i = 0; i < min(m, cnt); ++i) { res += ans[i]; } printf("CASE #%d: %lld\n",cas,res); } return 0; }
void balance() { if (right.size() > left.size() + 1) { left.insert(*right.begin()); right.erase(right.begin()); } if (left.size() > right.size()) { right.insert(*left.rbegin()); left.erase(--left.end()); } }
int main(int argc,char *argv[]) { int i,n; char str[20]; scanf("%d",&n); getchar(); for(i=0;i<n;i++) { scanf("%s",str); switch(str[1]) { case 'o': if(s.empty()) printf("Invalid\n"); else { int key=s.top(); s.pop(); printf("%d\n",key); if(key>*lower.begin()) { upper.erase(upper.find(key)); } else { lower.erase(lower.find(key)); } if(s.empty()) mid=0; else Adjust(&mid); } break; case 'e': if(s.empty()) printf("Invalid\n"); else printf("%d\n",mid); break; case 'u': int key; scanf("%d",&key); s.push(key); if(key>mid) upper.insert(key); else lower.insert(key); Adjust(&mid); break; } } return 0; }
void add(ll x){ multiset<ll>::iterator it; multiset<ll>::iterator jt; if (cnt == 0){ m2.insert(x); cnt++; c2++; printf("%lld\n",x); return; } jt = m2.begin(); if (cnt == 1){ if (x <= *jt){ m1.insert(x);cnt++;c1++; } else { m1.insert(*jt);m2.erase(jt); m2.insert(x);cnt++;c1++; } print(); return; } it = m1.end(); it--; if (cnt%2){ if (x <= *it){ m1.insert(x); c1++; } else { m2.insert(x); c1++; jt = m2.begin(); m1.insert(*jt); m2.erase(jt); } } else { if (x >= *jt){ c2++; m2.insert(x); } else { c2++; m1.insert(x); it = m1.end(); it--; m2.insert(*it); m1.erase(it); } } cnt++; print(); }
void median_ele() { //cout<<*s1.rbegin()<<endl; //printf("%d\n",(*s1.rbegin())); dint(*s1.rbegin()); s1.erase(s1.find(*s1.rbegin())); if(s1.size()<s2.size()) { s1.insert(*s2.begin()); s2.erase(s2.begin()); } }
void upd(){ while(SZ(up)<SZ(down)+1){ int t=*down.begin(); up.insert(t); down.erase(down.begin()); } while(SZ(up)>SZ(down)+1){ int t=*up.begin(); down.insert(t); up.erase(up.begin()); } }
void Adjust(int* mid)//调整中位数 { if(upper.size()>lower.size()) { lower.insert(*upper.begin()); upper.erase(upper.begin()); } else if(lower.size()>upper.size()+1) { upper.insert(*lower.begin()); lower.erase(lower.begin()); } (*mid)=*lower.begin(); }
int main() { ifstream in; ofstream out; in.open("input.txt"); out.open("output.txt"); int count=0; in >> M >> N; char t; for(int i=0; i<M; i++){ in >> t; parolaChars.insert(t); } in >> stringa; for(int i=0; i<M; i++){ it = parolaChars.find (stringa[i]); if(it!=parolaChars.end()) parolaChars.erase (it); else parolaCharsUnused.insert(stringa[i]); } if(parolaChars.empty()) count++; for(int i=M; i<N; i++){ it = parolaCharsUnused.find (stringa[i-M]); if(it!=parolaCharsUnused.end()) parolaCharsUnused.erase (it); else parolaChars.insert(stringa[i-M]); it = parolaChars.find (stringa[i]); if(it!=parolaChars.end()) parolaChars.erase (it); else parolaCharsUnused.insert(stringa[i]); //cout << parolaChars.size() << " " << stringa[i-M] << " " << stringa[i] << endl; if(parolaChars.empty()) count++; } out << count; in.close(); out.close(); return 0; }
int main(){ #ifndef ONLINE_JUDGE freopen("in","r",stdin); #endif scanf("%d",&n); int maxScore=0,minScore=2*n; For(i,n)scanf("%d",&a[i]); For(i,n)scanf("%d",&b[i]); sort(b+1,b+n+1); For(i,n)st.insert(a[i]); for(int i=n; i; i--){ it=st.upper_bound(b[i]); if(it!=st.end()){ maxScore+=2; st.erase(it); mark[i]=1; } } For(i,n)if(!mark[i]){ it=st.lower_bound(b[i]); if(it!=st.end()){ maxScore++; st.erase(it); } } st.clear(); memset(mark,0,sizeof(mark)); sort(a+1,a+n+1); For(i,n)st.insert(b[i]); for(int i=n; i; i--){ it=st.upper_bound(a[i]); if(it!=st.end()){ minScore-=2; st.erase(it); mark[i]=1; } } For(i,n)if(!mark[i]){ it=st.lower_bound(a[i]); if(it!=st.end()){ minScore--; st.erase(it); } } printf("%d %d\n",maxScore,minScore); return 0; }
void add_ele(int x) { if(s1.size()==s2.size()) { s2.insert(x); s1.insert(*s2.begin()); s2.erase(s2.begin()); } else if(s1.size()>s2.size()) { s1.insert(x); s2.insert(*s1.rbegin()); s1.erase(s1.find(*s1.rbegin())); } }
void adjust(){ long long val; if(!minset.empty()){ if(!maxset.empty()){ if(maxset.size() > minset.size()){ it = maxset.begin(); val = *it; maxset.erase(it); minset.insert(val); adjust(); } else if(minset.size() - maxset.size() > 1){ it = minset.end(); it--; val = *it; minset.erase(it); maxset.insert(val); adjust(); } } else{ if(minset.size() > 1){ it = minset.end(); it--; val = *it; minset.erase(it); maxset.insert(val); adjust(); } } } else{ if(!maxset.empty()){ it = maxset.begin(); val = *it; maxset.erase(it); minset.insert(val); adjust(); } } }
void del_photo(int t, int phi, int lambda){ auto dlt = dist(pos[t], make_pair(phi, lambda)); phi = dlt.first; lambda = dlt.second; auto p = make_pair(t, make_pair(phi, lambda)); taken.erase(taken.find(p)); }
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; }
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; }
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 ; }