int earliest(int phi, int lambda){ auto it = taken.begin(); auto nit = it; ++nit; pii pp = make_pair(phi, lambda); for (itn t = 0; t < T; t++){ while (nit != taken.end() && nit->first <= t) ++it, ++nit; auto p = dist(pos[t], pp); int old = it->first; if (abs(p.first) > d || abs(p.second) > d) continue; auto q = it->second; if (abs(q.first - p.first) > 1ll * w * (t - old)) continue; if (abs(q.second - p.second) > 1ll * w * (t - old)) continue; if (nit != taken.end()){ old = nit->first; q = nit->second; if (abs(q.first - p.first) > -1ll * w * (t - old)) continue; if (abs(q.second - p.second) > -1ll * w * (t - old)) continue; } return t; } return -1; }
int main() { int n,x; while(scanf("%d%d",&n,&x)+1) { st.clear(); for(int i=0; i<n; i++) { int x; scanf("%d",&x); st.insert(x); } for(;;) { it=upper_bound(st.begin(),st.end(),x); if(it!=st.end()) { x+=2; st.erase(it); } else break; } printf("%d\n",x+st.size()); } return 0; }
void dolnasrednia(int srednia) { multiset<int> ocenycopy = oceny; set<int>::iterator itbeg; itbeg=ocenycopy.begin(); while(itbeg!=ocenycopy.end()) { if((*itbeg)<=srednia) { pair <set<int>::iterator,set<int>::iterator> zasieg; zasieg=ocenycopy.equal_range(*itbeg); for(set<int>::iterator it=zasieg.first; *it< *zasieg.second;it++) { dolnasr.insert(*it); ocenycopy.erase(it); } int sumael=0; for(set<int>::iterator it=dolnasr.begin();it!=dolnasr.end();it++) { sumael+=(*it); } srednia=floor(sumael/dolnasr.size()); itbeg=ocenycopy.begin(); continue; } itbeg++; } int sumael=0; for(set<int>::iterator it=dolnasr.begin();it!=dolnasr.end();it++) { sumael+=(*it); } skroc(sumael, dolnasr.size()); }
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; }
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; }
void solve(){ s(n) sumSet.clear(); for(int i=0;i<(2<<n-1);++i){ ll x; sll(x) sumSet.insert(x); } vector<int> arr,ssums; for(multiset<int>::iterator itr=sumSet.begin();itr!=sumSet.end();itr++){ if(*itr == 0) continue; int e=*itr; int sp=ssums.size(); fr(i,0,sp) ssums.PB(ssums[i]+e); for(int i=sp;i<ssums.size();i++){ multiset<int>::iterator tmp=sumSet.find(ssums[i]); if(tmp!=sumSet.end()) sumSet.erase(tmp); } ssums.PB(e); arr.PB(e); if(arr.size() == n) break; } fr(i,0,arr.size()) printf("%d ",arr[i]); printf("\n"); }
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; }
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 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(); }
multiset<Node,NodeCompare>::iterator FindNode(const multiset<Node, NodeCompare>& nodes, int i, int j) { for (set<Node, NodeCompare>::iterator it = nodes.begin(); it != nodes.end(); ++it) { if (it->x == i && it->y == j) { return it; } } return nodes.end(); }
int main(){ n=getint(); long long ans=0; while(n--){ m=getint(); while(m--)S.insert(getint()); ans+=*(--S.end())-*S.begin(); S.erase(--S.end()); S.erase(S.begin()); }cout<<ans<<endl; return 0; }
int compare(multiset<int>& l, multiset<int>& r) { auto iterl=l.begin(), iterr=r.begin(); while (iterl!=l.end() && iterr!=r.end()) { if (*iterl<*iterr) return -1; else if (*iterl>*iterr) return 1; else { iterl++; iterr++; } } if (iterl==l.end() && iterr==r.end()) return 0; else if (iterl==l.end()) return -1; else return 1; }
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 delsegment(const int id, const vector<seg> &a) { pdd tmpp; set<seg>::iterator nxt = next (where[id]), prv = prev (where[id]); if (nxt != s.end() && prv != s.end() && abs(nxt->theta-prv->theta) > EPS) { tmpp = inspoint(*prv, a[id]); //debug("pushing1", nxt->id, prv->id, tmpp.first, nxt->p.x, nxt->q.x); if(inrange(*nxt, *prv, tmpp.first) && tmpp.first > cur_x) e.push(event(tmpp.first, tmpp.second, 0, nxt->id, prv->id)); } //debug("removing", id); s.erase (where[id]); }
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(); } } }
int main() { int n, m, p, b; scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%d%lld", &A[i].x, &A[i].t); ++A[i].t; A[i].idx = i; } sort(A, A + n, [&] (Frog A, Frog B) {return A.x > B.x;}); for (int i = 0; i < n; ++i) root -> set(A[i].x, min(A[i].x + A[i].t, (LL)INF), i); while (m--) { scanf("%d%d", &p, &b); int frog = root -> at(p); if (frog == -1) mos.insert({p, b}); else { A[frog].t += b; ++A[frog].mos_count; auto it = mos.lower_bound({A[frog].x, 0}); while (it != mos.end() and it -> first < A[frog].x + A[frog].t) { A[frog].t += it-> second; ++A[frog].mos_count; mos.erase(it); it = mos.upper_bound({A[frog].x, 0}); } root -> set(A[frog].x, min(A[frog].x + A[frog].t,(LL) INF), frog); } } sort(A, A + n, [&] (Frog A, Frog B) {return A.idx < B.idx;}); for (int i = 0; i < n; ++i) printf("%d %lld\n", A[i].mos_count, A[i].t - 1); }
const ex node::evaluate(const symbol &x, unsigned grad) const { static map<node,ex> catalog; // lookup table for already evaluated subnodes static unsigned prev_grad = 0; // grad argument that the catalog has been build for if (grad>prev_grad) { // We haven't computed far enough last time. Our catalog cannot cope with // the demands for this value of grad so let's clear it. catalog.clear(); prev_grad = grad; } ex product = 1; // accumulator for all the children for (multiset<child>::const_iterator i=children.begin(); i!=children.end(); ++i) { map<node,ex>::iterator pos = catalog.find(i->first); if (pos==catalog.end()) { pos = catalog.insert(map<node,ex>::value_type(i->first,i->first.evaluate(x,grad).series(x==0,grad).expand())).first; if (grad<prev_grad) { // We have just spoiled the catalog by inserting a series computed // to lower grad than the others in it. So let's make sure next time // we don't use one of the newly inserted ones by bumping prev_grad // down to the current value of grad. prev_grad = grad; } } if (!i->second) product *= pos->second; else product *= -div_part(pos->second,x,grad); } return (product * vert->evaluate(x,grad)); }
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() { #ifdef HOME freopen("input.txt", "r", stdin); #endif scanf("%d%d", &n, &k); i64 s = 0; forn(i, n) scanf("%d", &b[i]), s += b[i]; forn(i, n) scanf("%d", &a[i]); sort(a, a+n); f.insert(b, b+n); ford(i, n) { auto it = f.lower_bound(a[i]); if (it == f.end()) { if (k == 0) { printf("NIE\n"); return 0; } add += a[i]; --k; a[i] = 0; } else { a[i] = *it - a[i]; f.erase(it); } }
int main() { for(;;) { s.clear(); scanf("%d %d",&dimx,&dimy); if(dimx==0 && dimy==0) break; scanf("%d",&n); for(int i=0; i<n; ++i) scanf("%d %d",&x[i],&y[i]); x[n]=x[0]; y[n]=y[0]; for(int p=0; p<n; ++p) { double rez=solve(p); if(fabs(rez)<EPS) { for(int i=0,j=n;i<j;++i,--j) { swap(x[i],x[j]); swap(y[i],y[j]); } rez=solve(p); } s.insert(make_pair(rez,p)); } for(set<pair<double,int> >::iterator is=s.begin();is!=s.end(); ++is) printf("%03d%14.2lf\n", is->yy+1, is->xx); printf("\n"); } return 0; }
int main(){ int t,n; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=0;i<n;i++){ int h,w; scanf("%d%d",&h,&w); v[i].get(h,w,0); } for(int i=0;i<n;i++){ int h,w; scanf("%d%d",&h,&w); v[i+n].get(h,w,1); } sort(v,v+2*n,cmp); m.clear(); int ans=0; for(int i=0;i<2*n;i++){ // cout<<v[i].id<<" "<<v[i].h<<" "<<v[i].w<<endl; if(v[i].id==0) m.insert(v[i].w); else{ it=m.lower_bound(v[i].w); if(it!=m.end()){ m.erase(it); ans++; } } } printf("%d\n",ans); } return 0; }
int main() { scanf( "%d %d", &C, &G ); for ( i = 0; i < C + G; i++ ) { scanf( "%d %d", &j, &k ); ls.push_back( make_pair( k, ( i < C ) ? -j : j ) ); } sort( ls.rbegin(), ls.rend() ); for ( i = 0; i < C + G; i++ ) if ( ls[i].second > 0 ) S.insert( ls[i].second ); else { /* It's a cow */ set< int >::iterator it = S.lower_bound( -ls[i].second ); if ( it != S.end() ) { sol += *it; S.erase( it ); } else { sol = -1; break; } } printf( "%lld\n", sol ); return 0; }
void solve(int idx) { if(flag) puts(""); flag=1; scanf("%d",&N); printf("Case #%d:\n", idx); Ms.clear(); while(N--) { int L,R; scanf("%d %d",&L,&R); Point tmp; tmp.L=L;tmp.R=R; it=Ms.lower_bound(tmp); if(it==Ms.begin()||(--it)->R>R) { Ms.insert(tmp); it=Ms.upper_bound(tmp); while(it!=Ms.end()&&it->R>=tmp.R) { Ms.erase(it++); } } printf("%d\n",Ms.size()); } }
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); }
int main() { bool check(int,int,int,int); while (1) { int n,m; scanf("%d%d",&n,&m); if (!n && !m) break; a.clear(); int ans=0; for (int i=1;i<=n;i++) { int x; scanf("%d",&x); ans+=x; a.insert(x); } for (int i=1;i<=m;i++) { int x; scanf("%d",&x); data k=a.find(x); if (k==a.end()) ans+=x; else a.erase(k); } printf("%d\n",ans); } system("pause"); return(0); }
void process_incoming(multiset<order, IN> &incoming, multiset<order, WAIT> &waiting, int seq) { int size, price; cin >> size >> price; auto top_waiting = waiting.begin(); for ( ; size && top_waiting != waiting.end(); ++top_waiting) { if (can_trade(price, top_waiting, waiting)) { int trade_price = top_waiting->price; int trade_size = min(top_waiting->size, size); cout << "TRADE " << trade_size << " " << trade_price << '\n'; size -= trade_size; top_waiting->size -= trade_size; price2size[trade_price] -= trade_size; if (!top_waiting->size) gone.insert(top_waiting->seq); else break; } else break; } waiting.erase(waiting.begin(), top_waiting); if (size) { auto p2s_pit = price2size.insert({price, size}); if (!p2s_pit.second) p2s_pit.first->second += size; auto it = incoming.insert(incoming.begin(), {price, seq, size}); add_seq2iter_map(seq, it, &incoming); } else gone.insert(seq); }
int main() { freopen("a", "r", stdin); scanf("%d", &n); long long ans = 0; for (int i = 1; i <= n; i++) { int k, x; scanf("%d", &k); while (k--) { scanf("%d", &x); myset.insert(x); } /*for (it = myset.begin(); it != myset.end(); it++) cout << *it << " "; cout << endl;*/ it1 = myset.begin(); it2 = myset.end(); it2--; int tmp = *it2 - *it1; //cout << tmp << endl; ans += (long long)tmp; myset.erase(it1); myset.erase(it2); } cout << ans << endl; return 0; }
float CFeature::setThreshold(multiset<CKeypoint2D>& grid) { float ave = 0.0; float ave2 = 0.0; multiset<CKeypoint2D>:: iterator begin = grid.begin(); int count = 0; while (begin != grid.end()) { ave += begin->response; ave2 += begin->response * begin->response; ++count; ++begin ; } if(count == 0) count = 1; ave /= count; ave2 /= count; ave2 = sqrt(max(0.0f, ave2 - ave * ave)); float threshold = ave + ave2; return threshold; }
double findMedian() { if (Large.size()>Small.size()) return *Large.begin(); else return (*Large.begin()+*(--Small.end()))*1.0/2.0; }