poj2010(优先队列)

来源:互联网 发布:计算概率的软件 编辑:程序博客网 时间:2024/05/21 20:23

Moo University - Financial Aid
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 9829 Accepted: 2878

Description

Bessie noted that although humans have many universities they can attend, cows have none. To remedy this problem, she and her fellow cows formed a new university called The University of Wisconsin-Farmside,"Moo U" for short. 

Not wishing to admit dumber-than-average cows, the founders created an incredibly precise admission exam called the Cow Scholastic Aptitude Test (CSAT) that yields scores in the range 1..2,000,000,000. 

Moo U is very expensive to attend; not all calves can afford it.In fact, most calves need some sort of financial aid (0 <= aid <=100,000). The government does not provide scholarships to calves,so all the money must come from the university's limited fund (whose total money is F, 0 <= F <= 2,000,000,000). 

Worse still, Moo U only has classrooms for an odd number N (1 <= N <= 19,999) of the C (N <= C <= 100,000) calves who have applied.Bessie wants to admit exactly N calves in order to maximize educational opportunity. She still wants the median CSAT score of the admitted calves to be as high as possible. 

Recall that the median of a set of integers whose size is odd is the middle value when they are sorted. For example, the median of the set {3, 8, 9, 7, 5} is 7, as there are exactly two values above 7 and exactly two values below it. 

Given the score and required financial aid for each calf that applies, the total number of calves to accept, and the total amount of money Bessie has for financial aid, determine the maximum median score Bessie can obtain by carefully admitting an optimal set of calves. 

Input

* Line 1: Three space-separated integers N, C, and F 

* Lines 2..C+1: Two space-separated integers per line. The first is the calf's CSAT score; the second integer is the required amount of financial aid the calf needs 

Output

* Line 1: A single integer, the maximum median score that Bessie can achieve. If there is insufficient money to admit N calves,output -1. 

Sample Input

3 5 7030 2550 2120 205 1835 30

Sample Output

35

好题,一开始折磨了我很久,后来参考了网上的代码才写出来。

题意:奶牛学校招生,c头奶牛报名,要选n头(n为奇数),学校是义务制,所以每头奶牛的学费都由学校负责。每头奶牛都由自己的考试分数和它需要花的学费,学校总共有f的资金,问合法招生方案中中间分数(即排名第(n+1)/2)最高的是多少。

思路:先按分数排序,要求这个最大的中位数,所以它两边的数字对其没有影响,只要两边分别选择(n-1)/2头牛并且加上这头牛,他们要求的经济援助之和最小,所以把这个经济援助就分为了3部分,动态的求出dpl[i](1到i挑选(n-1)/2头牛所需的经济援助的最小和)和dpr[i](i到c挑选(n-1)/2头牛所需的经济援助的最小和),从高分到低分查找只要满足3部分的和满足小于f就可以。



/*先按分数排序,要求这个最大的中位数,所以它两边的数字对其没有影响,只要两边分别选择(n-1)/2头牛并且加上这头牛,他们要求的经济援助之和最小,所以把这个经济援助就分为了3部分,动态的求出dpl[i](1到i挑选(n-1)/2头牛所需的经济援助的最小和)和dpr[i](i到c挑选(n-1)/2头牛所需的经济援助的最小和),从高分到低分查找只要满足3部分的和满足小于f就可以。*/#include<iostream>  #include<stdio.h>  #include<string.h>  #include<queue> #include<algorithm>using namespace std;#define MAX_C 101000int N,C,F;int dpl[MAX_C]={0},dpr[MAX_C]={0};priority_queue<int> q;struct Node{int c,f;};Node cow[MAX_C];bool cmp(Node a,Node b){if(a.c!=b.c)return a.c>b.c;return a.f<b.f;}void solve(){int half_N=(N-1)/2;int sum=0;int i;while(!q.empty())q.pop();for(i=1;i<=half_N;i++)   //这部分从左到右找每个对应的i从1到i选(n-1)/2头牛所需的经济援助的最小和{q.push(cow[i].f);sum+=cow[i].f;}dpl[half_N]=sum;for(i=half_N+1;i<=C;i++){if(cow[i].f<q.top())      //把大的替换成小的 {sum=sum-q.top()+cow[i].f;q.pop();q.push(cow[i].f);}dpl[i]=sum;}while(!q.empty())q.pop();sum=0;for(i=C;i>C-half_N;i--)           //这部分从右到左找每个对应的i从i到c选(n-1)/2头牛所需的经济援助的最小和{q.push(cow[i].f);sum+=cow[i].f;}dpr[C-half_N+1]=sum;for(int i=C-half_N;i>=1;i--){if(cow[i].f<q.top()){sum=sum-q.top()+cow[i].f;q.pop();q.push(cow[i].f);}dpr[i]=sum;}for(i=half_N+1;i<=C-half_N;i++){if(cow[i].f+dpl[i-1]+dpr[i+1]<=F){cout<<cow[i].c<<endl;return;}}cout<<-1<<endl;return;}int main(){cin>>N>>C>>F;for(int j=1;j<=C;j++)cin>>cow[j].c>>cow[j].f;sort(cow+1,cow+1+C,cmp);solve();}  







原创粉丝点击