Moo University - Financial Aid

http://poj.org/problem?id=2010

Moo University - Financial Aid
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 12189   Accepted: 3609

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 题目大意:

美国新建立了一个大学,能够给N(奇数)个学生提供助学金,但是该学校有点穷,最多能提供助学金数额为F。现在总共有C个学生可待选择,给出了这些学生的成绩以及相应的助学金,然而学校希望这个N个学生的成绩的中位数尽可能地大,求这个中位数的最大值。

输入:第一行, N(1 <= N <= 19,999),C(N <= C <= 100,000),  F( 0 <= F <= 2,000,000,000);
            2...C+1行,每行两个数,score(1<=score<=2000000000),  aid(0 <= aid <=100,000)。
输出:可能的最大的中位数(成绩)。如果资金不足以承受这些学生则输出 -1。

思路:这题很明显是二分,但是二分的对象要清楚,刚开始我直接枚举0-2000000000    发现是错的  因为有的数根本不在题目中输入的数据内

怎么才能解决这个问题呢?  枚举下标  对的  就是枚举下标  我们把数组按照第一个数的大小从小到大排序  但是有人会问了  怎么找出小于当前值 但是又使得第二个数尽可能的小呢

方法是把第二个数的大小再从小到大排序 这样取出来的肯定就是尽可能的小了

看代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn=1e5+5;
struct Node
{
    ll v,w;
};
Node a[maxn];
Node b[maxn];
ll N,C,F;
bool cmp(const Node x,const Node y)//按照w值从小到大排  为了找出符合条件的左右两边的数
{
    return x.w<y.w;
}
bool cmp1(const Node x,const Node y)//v值从小到大排 为了使用二分
{
    return x.v<y.v;
}
bool judge(ll x,ll mid)
{
    int flag=0;//如果有和x相等的 留下一个
    ll M=F;
    M-=b[mid].w;//!!!这里刚开始忘记减了  找了很久很久的Bug  差点没做出来
    ll ec1=N>>1;// 一半比x小 一半比x大
    ll ec2=N>>1;
    for(ll i=0;i<C;i++)
    {
        if(a[i].v<x&&ec1)//优先取w值小的v
        {
            ec1--;
            M-=a[i].w;
        }
        else if(a[i].v>x&&ec2)
        {
            ec2--;
            M-=a[i].w;
        }
        else if(a[i].v==x)//想等的话 除去本身一个
        {
            if(flag)
            {
                if(ec1)
                {
                    ec1--;
                    M-=a[i].w;
                }
                else if(ec2)
                {
                    ec2--;
                    M-=a[i].w;
                }
            }
            else flag=1;
        }
    }
    if(ec1==0&&ec2==0&&M>=0) return true;
    return false;
}
int main()
{
    scanf("%lld%lld%lld",&N,&C,&F);
    for(ll i=0;i<C;i++)
    {
        scanf("%lld%lld",&a[i].v,&a[i].w);
        b[i]=a[i];
    }
    sort(a,a+C,cmp);
    sort(b,b+C,cmp1);
    ll l=0,r=C-1;
    ll ans=-1;
    while(l<=r)
    {
        ll mid=(l+r)>>1;//枚举下标
        //cout<<"mid="<<mid<<endl;
        if(judge(b[mid].v,mid))
        {
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    if(ans!=-1)
    printf("%lld\n",b[ans].v);
    else printf("-1\n");
    //cout<<b[ans].v<<endl;
    return 0;
}

 

上一篇:为什么方程与方程之间可以相互加减?


下一篇:登录界面01