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

Source

USACO 2004 March Green

题意:从C有奶牛中选N头,给出它们的分数scores和资助aid,要是这N头牛的总资助不超过F,同时它们中分数的中位数最大。求这个最大的中位数。

思路:按照分数排序,枚举每头牛作为中位数,计算牛i前面N/2头牛的最小资助和后面N/2牛的最小资助(用到优先队列)。最后从后往前找第一个满足l[i]+r[i]+cow[i].second<=F的即为答案。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

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

int main()
{
    int i,j;
    while (~sfff(n,c,f))
    {
        FRL(i,0,c)
        sff(cow[i].first,cow[i].second);
        sort(cow,cow+c);
        int half=n/2;
        int sum=0;
        FRL(i,0,c)
        {
            if (Q.size()==half)
                l[i]=sum;
            else
                l[i]=INF;
            sum+=cow[i].second;
            Q.push(cow[i].second);
            if (Q.size()>half)
            {
                sum-=Q.top();
                Q.pop();
            }
        }
        sum=0;
        while (!Q.empty()) Q.pop();
        FREE(i,c-1,0)
        {
            if (Q.size()==half)
                r[i]=sum;
            else
                r[i]=INF;
            sum+=cow[i].second;
            Q.push(cow[i].second);
            if (Q.size()>half)
            {
                sum-=Q.top();
                Q.pop();
            }
        }
        FREE(i,c-1,0)
        {
            if (l[i]+cow[i].second+r[i]<=f)
                break;
        }
        if (i>=0)
            pf("%d\n",cow[i].first);
        else
            pf("-1\n");
    }
    return 0;
}

第二种方法,采用二分:

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct Cow
{
    int s,f;
    int id;
}cow_s[maxn],cow_f[maxn];
int n,f,c,half;

int cmp_s(Cow a,Cow b)
{
    return a.s<b.s;
}

int cmp_f(Cow a,Cow b)
{
    return a.f<b.f;
}

int ok(int mid)
{
    int i,ans;
    int l=0,r=0,sum=cow_s[mid].f;
    FRL(i,0,c)
    {
        if (l<half&&cow_f[i].id<mid&&(sum+cow_f[i].f)<=f)
        {
            l++;
            sum+=cow_f[i].f;
        }
        else if (r<half&&cow_f[i].id>mid&&(sum+cow_f[i].f<=f))
        {
            r++;
            sum+=cow_f[i].f;
        }
    }
    if (l<half&&r<half)
        return -1;
    else if (l<half)
        return 1;
    else if (r<half)
        return 2;
    else
        return 3;
}

void solve(int l,int r)
{
    int mid;
    int ans;
    while (r-l>1)
    {
        mid=(l+r)>>1;
        int flag=ok(mid);
        if (flag==-1)
        {
            ans=-1;
            break;
        }
        else if (flag==1)
            l=mid;
        else if (flag==2)
            r=mid;
        else
        {
            ans=cow_s[mid].s;
            l=mid;
        }
    }
    pf("%d\n",ans);
}

int main()
{
    int i,j;
    while (~sfff(n,c,f))
    {
        FRL(i,0,c)
        sff(cow_s[i].s,cow_s[i].f);
        sort(cow_s,cow_s+c,cmp_s);
        FRL(i,0,c)
            cow_s[i].id=i;
        memcpy(cow_f,cow_s,sizeof(cow_s));
        sort(cow_f,cow_f+c,cmp_f);
        half=n/2;
        solve(0,c);
    }
    return 0;
}
时间: 2025-01-07 16:06:32

Moo University - Financial Aid (poj 2010 优先队列 或者 二分)的相关文章

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

Moo University - Financial Aid POJ 2010(枚举)

原题 题目链接 题目分析 题目要求取中位数,我们可以枚举以每个牛的分数作为中位数,然后选出最省钱的方案,最后只需要看花钱数不超过F中的最高的中位数分数即可.说一下具体实现,先对所有牛按分数从小到大排序,选出最省钱的方案可以用优先队列来选,设half=n/2,当优先队列的牛超过half个的时候就把一个花费最多的弹出去,这样从小到大遍历一遍就可以记录分数比中位数小的那些牛的花费,记为lower[i],从大到小遍历一遍就可以记录分数比中位数大的那些牛的花费,记为upper[i],最后这个方案的总花费就

挑战程序设计竞赛3.1习题:Moo University - Financial Aid POJ - 2010

(原题见POJ2010) 这道题我之前采用了优先队列+预处理的方法求解(https://www.cnblogs.com/jacobfun/p/12244509.html),现在用二分的办法进行求解. 一开始我很纳闷,采用二分求解本题,如果二分的mid值不符合条件,按照二分右边界应该为mid - 1(我采用前闭后闭的二分),那么如果mid + xxx(xxx大于0)可以呢?(考虑mid不行是因为左边最小加起来大了,mid ~ mid + xxx中有极小值,使得mid + xxx的左边可以满足,那么

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( 优先队列+二分查找)

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

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(优先队列(最小堆)+ 贪心 + 枚举)

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 初探数据结构 二叉堆

考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速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