二分使第K个数尽量大--poj2010


Language:
Moo University - Financial Aid
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3892 Accepted: 1159

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

错爆了,就是不知道哪错了。。
下面是AC代码:
# include <cstdio>
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std;
const int maxN=100005;
struct Node{ int c,s,id; };
int n,m,tot;
Node cows[maxN],cowc[maxN];
 
bool cmps(Node a,Node b) { return a.s<b.s; }
bool cmpc(Node a,Node b) { return a.c<b.c; }
 
int check(int limit)
{
    int left=0,right=0,all=cows[limit].c;
    for (int i=0;i<n;i++){
        if ((cowc[i].id<limit)&&(all+cowc[i].c<=tot)&&(left<m/2)){
            all+=cowc[i].c;left++;
        } else if ((cowc[i].id>limit)&&(all+cowc[i].c<=tot)&&(right<m/2)){
            all+=cowc[i].c;right++;
        }
    }
    if ((left<m/2)&&(right<m/2)) return -1;
    else if (left<m/2) return 1;
    else if (right<m/2) return 2;
    else return 0;
}
 
int main()
{
    scanf("%d%d%d",&m,&n,&tot);
    for (int i=0;i<n;i++) scanf("%d%d",&cows[i].s,&cows[i].c);
    sort(cows,cows+n,cmps);
    for (int i=0;i<n;i++) cows[i].id=i;
    memcpy(cowc,cows,sizeof(cows));
    sort(cowc,cowc+n,cmpc);
    int l=1,r=n,ans=-1;
    while (l<=r){
        int mid=(l+r)>>1;
        switch (check(mid)){
            case -1: { printf("-1\n"); return 0; }
            case  0: { ans=cows[mid].s; l=mid+1; break; }
            case  1: { l=mid+1; break;}
            case  2: { r=mid-1; }
        }
    }
    if (ans>=0) printf("%d\n",ans); else printf("-1\n");
    return 0;
}

下面是错误的,求指点:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int N,C,F;
struct node
{
    int score,cost,id;
} cow1[200100],cow2[200100];
bool cmp1(node a,node b)
{
    return a.score<b.score;
}
bool cmp2(node a,node b)
{
    return a.cost<b.cost;
}
bool can(int mid)
{
    int a=0,b=0,c=0,x=cow1[mid].score;
    int sum=F-cow1[mid].cost;
    int n=N/2;
    for(int i=1; i<=C; i++)
    {
        if(a<n&&cow2[i].score<x&&sum>=cow2[i].cost)
        {
            sum-=cow2[i].cost;
            a++;
        }
        else if(b<n&&cow2[i].score>x&&sum>=cow2[i].cost)
        {
            sum-=cow2[i].cost;
            b++;
        }
        else if(cow2[i].score==x&&sum>=cow2[i].cost&&cow2[i].id!=cow1[mid].id)
        {
            sum-=cow2[i].cost;
            c++;
        }
        if(a+b+c>=N-1)
            return true;
    }
    return false;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(cin>>N>>C>>F)
    {
        for(int i=1; i<=C; i++)
        {
            scanf("%d%d",&cow1[i].score,&cow1[i].cost);
            cow1[i].id=cow2[i].id=i;
            cow2[i].score=cow1[i].score;
            cow2[i].cost=cow1[i].cost;
        }
        sort(cow1+1,cow1+C+1,cmp1);
        if(N==1)
        {
            int ans=0,i;
            bool flag=false;
            for(i=C; i>=1; i--)
                if(cow1[i].cost<=F)
                {
                    flag=true;
                    break;
                }
            if(flag)
                cout<<cow1[i].score<<endl;
            else
                cout<<-1<<endl;
            continue;
        }
        sort(cow2+1,cow2+C+1,cmp2);
        int ans=-1;
        int l=0,r=C-1,mid;
        while(r>=l)
        {
            mid=(l+r)/2;
            if(can(mid))
            {
                ans=cow1[mid].score;
                l=mid+1;
            }
            else
                r=mid-1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值