贪心+优先队列

优先队列&贪心

最近遇到很多题目的贪心都用优先队列来优化,现在简单的讲一讲一个比较难想到的优化

  • Moo University - Financial Aid

    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.

题目大意:有C头牛要上大学,但是只有其中n头牛能够得到奖学金,要求这n头牛的中位数最大并且奖学金的总数不大于F

题解:首先对于成绩从大到小排序,枚举中位数,对于给定的中位数取出分数>该数并且所需奖金最小的前(n-1)/2 个 和小于该数并所需奖金最小的前 (n-1)/2个 看奖金总数是不是大于F

以这样的思路去写就没有问题,但是我前两次都超时了,原因是我在取奖金的时候放在了枚举中位数的循环里面,因此,为了优化这种情况,我们可以先保存下来对于每个中位数它的前后的奖金数,再去从大到小枚举中位数,这样就能规避超时的问题

下面看看代码:

#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <string>
#include <math.h>
#define inf 0x3f3f3f3f
using namespace std;
long long c, n, f;
struct stu
{
    long long score, aid;
    bool operator <(const stu &oth)const
    {
        return aid < oth.aid;
    }

} a[123456];
bool cmp(stu a, stu b)
{
    return a.score >b.score;
}
int main()
{
    int i, j, k;
    cin >> n >> c >> f;
    for ( i = 0; i < c; i++)
    {
        cin >> a[i].score >> a[i].aid;
    }
    sort(a, a + c,cmp);
    long long mid,sum=0,sum1[123456],flag=1,sum2[123456],sum3;
    stu temp;
    priority_queue<stu>que;
    for(i=0;i<c;i++){
    if(que.size()==(n-1)/2){
        sum1[i]=sum;
    }else sum1[i]=inf;
    sum+=a[i].aid;
    que.push(a[i]);
    if(que.size()>(n-1)/2){
        sum-=que.top().aid;
        que.pop();
    }
    }
    priority_queue<stu>que2;
    sum=0;
    for(j=c-1;j>=0;j--){
        if(que2.size()==(n-1)/2){
            sum2[j]=sum;
        }else sum2[j]=inf;
    sum+=a[j].aid;
    que2.push(a[j]);
    if(que2.size()>(n-1)/2){
        sum-=que2.top().aid;
        que2.pop();
    }
    }
    for(i=(n-1)/2;i<=c-(n-1)/2-1;i++){
            flag=1;
        if(a[i].aid+sum1[i]+sum2[i]<=f){
            flag=0;
            cout<<a[i].score<<endl;
            break;
        }
    }
    if(flag)cout<<-1<<endl;
    return 0;
}
全部评论

相关推荐

11-03 14:38
重庆大学 Java
AAA求offer教程:我手都抬起来了又揣裤兜了
点赞 评论 收藏
分享
没有offer的小土豆:专业面试一般是分配面试官然后联系你面试 应该是还没给你分配对应面试官
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务