Poj 2010-Moo University - Financial Aid

Moo University - Financial Aid

题目链接:http://poj.org/problem?id=2010

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9046   Accepted: 2640

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 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.

代码:

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int N=1e5+1;
typedef pair<int,int> P;
P a[N];
int lower[N],upper[N];
int main()
{
    int n,c,f;
    cin>>n>>c>>f;
    int half=n/2;
    for(int i=0;i<c;i++)
    cin>>a[i].first>>a[i].second;
    sort(a,a+c);
    {
        int total=0;
        priority_queue<int>q;
        for(int i=0;i<c;i++)
        {
            lower[i]=q.size()==half?total:0x3f3f3f3f;
            q.push(a[i].second);
            total+=a[i].second;
            if(q.size()>half)
            {
                total-=q.top();
                q.pop();
            }
        }
    }
    {
        int total=0;
        priority_queue<int>q;
        for(int i=c-1;i>=0;i--)
        {
            upper[i]=q.size()==half?total:0x3f3f3f3f;
            q.push(a[i].second);
            total+=a[i].second;
            if(q.size()>half)
            {
                total-=q.top();
                q.pop();
            }
        }
    }
    for(int i=c-1;i>=0;i--)
    {
        if(a[i].second+upper[i]+lower[i]<=f)
        {
            cout<<a[i].first<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}
时间: 2024-11-01 11:57:16

Poj 2010-Moo University - Financial Aid的相关文章

POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在,则输出-1(一开始因为没看见这个,wa了几次). 这个题的第一种做法就是用两个优先队列+贪心. /* * Created: 2016年03月27日 14时41分47秒 星期日 * Author: Akrusher * */ #include <cstdio> #include <cstdl

poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)

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 wish

poj -2010 Moo University - Financial Aid (优先队列)

http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U"大学学费非常昂贵,并非每一头小牛都能支付的起,很多小牛都需要经济援助,但是学校只有有限的资金F. "Moo U"大学只会从C个学生里选N个学生出来,(N是奇数),但是希望N头小牛CSAT得分的中位数越高越好.输入N,C,F 接下来C行,每行包括一个得分和需要申请的经济援助,输出符合条件

POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆

考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O(1)找到最大/最小值,并能持续维护. 复杂度 push() = O(logn); pop() = O(logn); BinaryHeap() = O(nlogn); 实现 数组下标从1开始的情况下,有 Parent(i) = i >> 1 LChild(i) = i << 1 RChi

poj 2010 Moo University - Financial Aid 大顶堆维护最小和

题意: 有c有牛,从中选(n-1)/2头,使他们的得分中位数最大且需要的资金援助和不超过f. 分析: 堆的运用大顶堆维护最小和. 代码: //poj 2010 //sep9 #include <iostream> #include <queue> #include <algorithm> using namespace std; const int maxN=100024; int dpl[maxN],dpr[maxN]; priority_queue<int&g

POJ - 2010 Moo University - Financial Aid 贪心+优先队列

题目大意:有C头牛,每头牛都有相应的分数和需求,要求在这C头牛中选出N头,使得这N头牛中的分数的中位数达到最大,且需求之和小于等于F 解题思路:先按成绩排序 再用两个数组保留最小需求之和 left数组保留第i个位置左边的 N/2个最小需求之和 right数组保留第i个位置右边的 N/2个最小需求之和 如何保留最小的需求之和呢,扫描两遍(左右),用优先队列保留N / 2个最小需求 最后只需要判断一下 left[i] + right[i] + 第i头牛的需求 <= F就可以了 #include<c

POJ 2010 Moo University - Financial Aid 堆的高级应用 -- 维护最小(最大和)

题目大意:有N头牛,每头牛两个权值,A和B.从这N头牛中选取C头牛,使得: 1.这些牛中A权值的中位数尽量大. 2.这些牛的B权值的和小于题中所给的F 输出这个最大的A权值的中位数:如果没有满足题意的解,就输出-1.值. 思路: 堆有一个神奇的功能.假设上图是一个数组,在B从A到C移动的过程中,利用大根堆可以维护出B在所有位置时,从A到B中选K个值的和的最小值,并在nlogn内得到答案. 方法如下:先把[A,A + K]的元素加入到一个大根堆中,记录它们的总和.之后让B不断向后循环,把B加入到大

Moo University - Financial Aid (poj 2010 优先队列 或者 二分)

Language: Default Moo University - Financial Aid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5551   Accepted: 1663 Description Bessie noted that although humans have many universities they can attend, cows have none. To remedy this p

poj2010 Moo University - Financial Aid

Moo University - Financial Aid 题意: 一个私立学校的学生要申请奖学金,而学校的金额有限.因此,学校希望在金额不超过F的情况下从C中选得N对数. 给出三个数N,C,F.分别代表在C对数中要取得N对数. 而每对数分别代表成绩,跟申请金额.要求取得N对数中的总金额不超过F的条件下,然取得中间的以为学生的成绩最高.(N为even) 算法分析: 本题有两种解法,一种是用优先队列,一种是二分; 一.利用堆实现 先说堆的实现方法,我们能够现对头尾的N/2进行处理,由于头尾的N/

挑战程序设计竞赛2.4习题:Moo University - Financial Aid 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