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

题意:给你 c头牛和每头牛的智商、花费,要你选择 n头牛使这 n头牛智商的中位数最大,前提是花费总和不能超过 f

题解:先对每头牛的智商从小到大排序,然后枚举智商的中位数,对于某个中位数i,左边和右边各取花费最小的n/2头牛。怎么取呢?先用优先队列预处理每头牛左右两边n/2头牛花费最小值即可。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;

const int maxn = 100010;

priority_queue<int> p, q;
int n, c, f;
pair<int, int> cow[maxn];
int l[maxn], r[maxn];

int main()
{
    cin >> n >> c >> f;
    for (int i = 0; i < c; ++i)
        scanf("%d%d", &cow[i].first, &cow[i].second);
    sort(cow, cow+c);

    //预处理以i为中位数时左边n/2头牛花费的最小值
    int s = 0;
    for (int i = 0; i < c; ++i)
    {
        if (p.size() == n / 2)
            l[i] = s;
        p.push(cow[i].second);
        s += cow[i].second;
        if (p.size() > n / 2)
        {
            s -= p.top();
            p.pop();
        }
    }

    //预处理以i为中位数时右边n/2头牛花费的最小值
    s = 0;
    for (int i = c-1; i >= 0; --i)
    {
        if (q.size() == n / 2)
            r[i] = s;
        q.push(cow[i].second);
        s += cow[i].second;
        if (q.size() > n / 2)
        {
            s -= q.top();
            q.pop();
        }
    }

    //从大到小枚举中位数,满足题意就退出
    for (int i = c-(n+1)/2; i >= n/2; --i)
    {
        int sum = cow[i].second + l[i] + r[i];
        if (sum <= f)
        {
            cout << cow[i].first << endl;
            return 0;
        }
    }
    cout << "-1" << endl;
    return 0;
}

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-08-29 22:36:16

poj2010(Moo University - Financial Aid)优先队列的相关文章

poj2010 Moo University - Financial Aid

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

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

POJ2010 Moo University - Financial Aid(二分法)

题目地址 分析:如果用二分法,关键是score和aid分开排序,score排序是为了充分利用中位数的性质,这样就可以确定m左右必须各选N/2个,到这之后有人是用dp求最优解,可以再次按照aid排序一次,可以直接确定最优解(肯定是从最小的开始选择!): 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 int N, C, F; 6 const i

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行,每行包括一个得分和需要申请的经济援助,输出符合条件

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

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

挑战程序设计竞赛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

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 【堆预处理】

一.题面 POJ2010 二.分析 堆预处理 首先可以考虑吧随便取一个点,判断两侧的最小的总费用是多少,然后相加判断是否满足条件.如果直接判断会超时,所以需要用大根堆预处理一下.先看从分数最小的往最大的预处理,先取N/2个相加,并把他们都加入到堆中,先假设这个和值是最大的,然后不断往后扫描的过程中,不断更新大根堆的根值,以及它的和.反向预处理类似.比较容易出错的是选的范围,如果不用那些不可能到的点,可以随意点,但如果需要使用这些点,需要用足够大的值填充. 三.AC代码 1 #include <c