Test instructions
There is a C with cattle, from which to choose (n-1)/2 head, so that they score the median of the largest and need financial assistance and not more than F.
Analysis:
The use of heaps of large top stacks maintains minimal and.
Code:
POJ 2010//sep9#include <iostream> #include <queue> #include <algorithm>using namespace Std;const int Maxn=100024;int DPL[MAXN],DPR[MAXN];p riority_queue<int> q;struct cow{int s,f;} Cow[maxn];int cmp (Cow X,cow y) {return x.s>y.s;} int main () {int i,n,c,f;while (scanf ("%d%d%d", &n,&c,&f) ==3) {n= (n-1)/2;for (i=1;i<=c;++i) scanf ("%d%d" , &cow[i].s,&cow[i].f); sort (cow+1,cow+1+c,cmp); while (! Q.empty ()) q.pop (); int sum=0;for (i=1;i<=n;++i) Q.push (COW[I].F), Sum+=cow[i].f;dpl[n]=sum;for (i=n+1;i<=c;++i ) if (Cow[i].f>=q.top ()) dpl[i]=sum;else{sum=sum-q.top () +cow[i].f; Q.pop (); Q.push (COW[I].F);DP l[i]=sum;} Sum=0;while (! Q.empty ()) Q.pop (); for (i=c;i>=c-n+1;--i) Q.push (COW[I].F), Sum+=cow[i].f;dpr[c-n+1]=sum;for (i=c-n;i>=1;--i) if (Cow[i].f>=q.top ()) dpr[i]=sum;else{sum=sum-q.top () +cow[i].f; Q.pop (); Q.push (COW[I].F);DP r[i]=sum;} BOOL Ok=false;for (I=n+1;i<=c-n;++i) if (cow[i].f+dpl[i-1]+dpr[i+1]<=f) {ok=true;printf ("%d\n", COW[I].S); break;} if (!ok) printf (" -1\n");} return 0;}
POJ Moo university-financial Aid Large top heap maintenance min and