POJ 2010 (Priority queue

Source: Internet
Author: User

Sweep with priority queue to get the minimum value of the sum of the values greater than and less than the median, and sweep again to get the optimal solution

#include <iostream>#include<cstdio>#include<algorithm>#include<queue>#include<utility>#include<vector>#defineINF 0X3FFFFFFFusing namespaceStd;typedefLong Longll;intN,c,f;typedef pair<int,int>PII;Const intmaxv=1e5+ -;p Air<int,int> COW[MAXV];////score,aidll LOW[MAXV],UPP[MAXV];BOOLCMP (PII a,pii b) {returna.second<B.second;}intMain () {Freopen ("In.txt","R", stdin); CIN>>N>>C>>F;  for(intI=0; i<c;i++) scanf ("%d%d",&cow[i].first,&Cow[i].second); Sort (Cow,cow+b); ll Tol=0; Priority_queue<int>q1,q2; ll half=n/2;  for(intI=0; i<c;i++) {Low[i]= (Q1.size () ==half?Tol:inf);        Q1.push (Cow[i].second); Tol+=Cow[i].second; if(Q1.size () >half) {Tol-=Q1.top ();        Q1.pop (); }} Tol=0;  for(inti=c-1; i>=0; i--) {Upp[i]= (Q2.size () ==half?Tol:inf);        Q2.push (Cow[i].second); Tol+=Cow[i].second; if(Q2.size () >half) {Tol-=Q2.top ();        Q2.pop (); }    }    intans=-1;  for(intI=0; i<c;i++){        if(low[i]+upp[i]+cow[i].second<=F) ans=Cow[i].first; } cout<<ans<<Endl; return 0;}

POJ 2010 (Priority queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.