POJ2010(递增序列,求最优解)

Moo University - Financial Aid
Time Limit: 1000MS

Memory Limit: 30000K
Total Submissions: 11694

Accepted: 3455
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
    题目中提到寻求可以赞助学生的最大中位数,看到题目首先想到的是dfs和贪心,dfs不用看数据量大的一瞥,会TLE所以,就试着用贪心,贪心的主要问题是不容易找到中位数位置,前后贪心方式不同,
    所以,就想到了递增数列,其实也很好理解,如果没有钱数限制,那么我们就从后面往前找n/2个就好了,那么我们找出中位数位置钱的最优,如果可以那该中位数就是一种可能,枚举种种可能数据,从大到小,找到数据就可以了,找不到输出-1.
//贪心求解
//找到问题”可能答案“的最优情况
//枚举所有可能,枚举顺序与所需顺序相同
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#define MAXN 100005
using namespace std;

int mleft[MAXN];
int mright[MAXN];
int n,m,money;
struct node{
    int score;
    int need;
}stu[MAXN];

priority_queue<int> que;
priority_queue<int> qse;


bool cmp(node a,node b){
    if(a.score==b.score){
        return a.need>b.need;
    }
    return a.score<b.score;
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&money)){
        for(int i=1;i<=m;i++){
            scanf("%d%d",&stu[i].score,&stu[i].need);
        }
        sort(stu,stu+m+1,cmp);
        int sum=0;
        for(int i=1;i<=n/2;i++){
            que.push(stu[i].need);
            sum+=stu[i].need;
        }
        mleft[n/2] = sum;
        for(int i=n/2+1;i<=m-n/2;i++){
            int tmp = que.top();
            if(stu[i].need<tmp){
                que.pop();
                que.push(stu[i].need);
                mleft[i] = mleft[i-1]-tmp+stu[i].need;
            }
            else{
                mleft[i] =mleft[i-1];
            }
        }
        sum=0;
        for(int i=m;i>m-n/2;i--){
            qse.push(stu[i].need);
            sum+=stu[i].need;
        }
        mright[m-n/2+1] = sum;
        for(int i=m-n/2;i>n/2;i--){
            int tmp = qse.top();
            if(stu[i].need<tmp){
                qse.pop();
                qse.push(stu[i].need);
                mright[i] = mright[i+1]-tmp+stu[i].need;
            }
            else{
                mright[i] =mright[i+1];
            }
        }
        int flag = 0;
        for(int i=m-n/2;i>n/2;i--){
            if(money>=mleft[i-1]+stu[i].need+mright[i+1]){
                printf("%d\n",stu[i].score);
                flag = 1;
                break;
            }
        }
        if(!flag)
            printf("%d\n",-1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40488730/article/details/81562495