• poj 2010 Moo University


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

    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

    /*
    * @Author: Lyucheng
    * @Date:   2017-07-20 09:47:21
    * @Last Modified by:   Lyucheng
    * @Last Modified time: 2017-07-20 17:56:01
    */
    /*
     题意:给你C个奶牛参加考试的分数,和需要奖学金的数目,让你从中选N头奶牛,需要总奖学金的数目不超过F,并且分数的
        中位数最大
    
     思路:按照分数排序,二分中位数,有个问题就是判断问题可能会超时
     细节:判断的时候还有给他排一下序,这样才能去到最小的,还要特判n==1的时候
    */
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm> 
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAXN 100005
    
    using namespace std;
    
    struct Node{
        int score,cost;
        bool operator < (const Node & other) const{
            if(score==other.score) return cost<other.cost;
            return score < other.score;
        }
    }node[MAXN];
    int n,c,f;
    Node pos[MAXN];
    
    bool cmp(Node a,Node b){
        return a.cost<b.cost;
    }
    
    bool ok(int x){
        int sum=f-node[x].cost;//总的钱数
        int cur=0;
        for(int i=1;i<=c;i++){
            if(i==x) continue;
            pos[++cur]=node[i];
        }
        sort(pos+1,pos+cur+1,cmp);
        int cnt0=0,cnt1=0;//统计大于小于
        int cnt2=0;//统计等于
        for(int i=1;i<=cur;i++){
            if(sum>=pos[i].cost){
                if(pos[i].score==node[x].score){
                    sum-=pos[i].cost;
                    cnt2++;
                }else if(pos[i].score<node[x].score){
                    if(cnt0<n/2){
                        sum-=pos[i].cost;
                        cnt0++;
                    }
                }else if(pos[i].score>node[x].score){
                    if(cnt1<n/2){
                        sum-=pos[i].cost;
                        cnt1++;
                    }
                }
            }
        }
        // cout<<node[x].score<<" "<<cnt0<<" "<<cnt1<<" "<<cnt2<<endl;
        if(cnt0+cnt1+cnt2>=n-1) return true;
        return false;
    }
    
    int main(){
        // freopen("in.txt", "r", stdin);
        // freopen("out.txt", "w", stdout);
        while(scanf("%d%d%d",&n,&c,&f)!=EOF){
            for(int i=1;i<=c;i++){
                scanf("%d%d",&node[i].score,&node[i].cost);
            }
            if(n==1){
                sort(node+1,node+c+1,cmp);
                bool f=false;
                for(int i=c;i>=1;i--){
                    if(node[i].cost<=f){
                        printf("%d",node[i].score);
                        f=true;
                        break;
                    }
                }
                if(f==false){
                    puts("-1");
                }
                continue;
            }
            sort(node+1,node+c+1);
            int l=1,r=c,mid;
            int res=-1;
            while(l<=r){
                mid=(l+r)/2;
                if(ok(mid)==true){
                    l=mid+1;
                    res=node[mid].score;
                }else{
                    r=mid-1;
                }
            }
            printf("%d
    ",res);
        }
        return 0;
    }
  • 相关阅读:
    java实现RSA非对称加密
    lombok中的@Builder注解
    java实现大文件的分割与合并
    IDEA新建springboot选择DevTools
    bat命令自动配置java环境变量
    java实现发送邮件
    随记
    编译原理学习——FIRST和LASTVT
    国王的游戏
    JAVA类加载及NEW对象的过程
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/7212601.html
Copyright © 2020-2023  润新知