(堆操作) Moo University - Financial Aid (P2010)

来源:互联网 发布:网络优化测试工作 编辑:程序博客网 时间:2024/06/07 21:06

 一开始做这个还没有什么思路,后面看了一下别人的代码也就知道大概是怎么做了。


思路:1、先把输入的数据依score的顺序从小到大进行排序。

            2、从小到大的进行堆插入,当不满n/2个时就进行插入,如果满了,就插入比堆内最大值小的值,并进行更新

其中low[i]里就保存前i个中有n/2个相加的need数最小值。

           3、同理的从大小到的进行堆维护后得到high[i],其中保存的是后c-i+1个里n/2个最小值。

           4、从大到小进行中间值的判定,如果满足low[i-1]+p[i].need+high[i+1]<=price则输出答案p[i].score。



#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<ctype.h>#include<cstdlib>using namespace std;#define N 111111int n,c,price;int h[N];int low[N],high[N];int sum,r;struct my{int score,need;}p[N];bool cmp(my a,my b){return a.score<b.score;}void up(int i){int j;while (i>1){j=i/2;if (h[j]<h[i])swap(h[i],h[j]);elsebreak;i=j;}}void insert(int x){h[++r]=x;sum+=x;up(r);}void down (int i){int j;while (i<=r){j=i*2;if (j<r && h[j+1]>h[j])j++;if (h[j]>h[i])swap(h[j],h[i]);elsebreak;i=j;}}void del(int x){sum=sum+x-h[1];h[1]=x;down(1);}void put(){for (int i=1;i<=c;i++){cout<<p[i].score<<' '<<p[i].need<<endl;}cout<<endl;for (int i=1;i<=c;i++){cout<<low[i]<<' ';}cout<<endl<<endl;for (int i=1;i<=c;i++)cout<<high[i]<<' ';cout<<endl<<endl;}int main(){  freopen("c:\in.txt","r",stdin);int i,j,k;cin>>n>>c>>price;for (i=1;i<=c;i++){scanf("%d%d",&p[i].score,&p[i].need);}sort(p+1,p+1+c,cmp);memset(h,0,sizeof(h));sum=r=0;n/=2;for (i=1;i<=n;i++){insert(p[i].need);}low[n]=sum;for (i=n+1;i<=c-n;i++){        if (p[i].need<h[1])del(p[i].need);low[i]=sum;}memset(h,0,sizeof(h));sum=r=0;for (i=c;i>c-n;i--){insert(p[i].need);}high[c-n+1]=sum;for (i=c-n;i>=n;i--){        if (p[i].need<h[1])del(p[i].need);high[i]=sum;}//put();for (i=c-n;i>n;i--) if (low[i-1]+high[i+1]+p[i].need<=price){cout<<p[i].score<<endl;//system("pause");return 0;}cout<<"-1"<<endl;return 0;}



Language:
Moo University - Financial Aid
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 3435 Accepted: 1021

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

Hint

Sample output:If Bessie accepts the calves with CSAT scores of 5, 35, and 50, the median is 35. The total financial aid required is 18 + 30 + 21 = 69 <= 70.

Source

USACO 2004 March Green


原创粉丝点击