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