POJ 2010
Posted kongbb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2010相关的知识,希望对你有一定的参考价值。
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.
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
* 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 70 30 25 50 21 20 20 5 18 35 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.
暴力+优先队列
枚举所有的中位数
用优先队列维护出当前中位数的左侧n/2个数的最小总代价 并记录 左侧代价 和 中位数
再反向维护出右侧n/2个数的最小总代价并加到之前求出来的左侧数的总代价中去,就是最小总代价
中位数的代价也要加进去,至于具体加到左堆还是右堆看个人习惯。
我的代码实现是加进了左堆。
#include<cstdio> #include<queue> #include<algorithm> using namespace std; pair<long long, long long> arr[100005]; pair<long long, long long> ans[100005]; int main(){ long long n, m, d; scanf("%lld%lld%lld",&n,&m,&d); for(int i=0;i<m;i++) scanf("%lld%lld",&arr[i].first,&arr[i].second); sort(arr,arr+m); long long sum = 0; priority_queue<long long>Q; for(int i=0;i<n/2;i++){//初始化 sum += arr[i].second; Q.push(arr[i].second); } for(int i=n/2;i<m-n/2;i++){//枚举 中位数 sum += arr[i].second;//将当前的数作为中位数加入队列 Q.push(arr[i].second); ans[i-n/2].first = arr[i].first;// i-n/2 是当前中位数所在区间的起点 ans[i-n/2].second = sum; // arr[i-n/2]并不一定在可行解中 只是一个相对唯一的位置 sum -= Q.top();//出队为下一个中位数的处理做准备 Q.pop(); } while(!Q.empty())Q.pop(); sum = 0; for(int i=m-1;i>=m-n/2;i--){ sum += arr[i].second; Q.push(arr[i].second); } for(int i=m-n/2-1;i>=n/2;i--){ ans[i-n/2].second += sum; sum += arr[i].second; Q.push(arr[i].second); sum -= Q.top(); Q.pop(); } //sort(ans,ans+(m-n)); for(int i=m-n;i>=0;i--) if(ans[i].second<=d){ printf("%lld ",ans[i].first); return 0; } printf("-1 "); return 0; }
以上是关于POJ 2010的主要内容,如果未能解决你的问题,请参考以下文章
vc++2010设置和c#一样的代码段,vs2010 两下tab设置