POJ - 2010 Moo University - Financial Aid 贪心+优先队列

题目大意:有C头牛,每头牛都有相应的分数和需求,要求在这C头牛中选出N头,使得这N头牛中的分数的中位数达到最大,且需求之和小于等于F

解题思路:先按成绩排序

再用两个数组保留最小需求之和

left数组保留第i个位置左边的 N/2个最小需求之和

right数组保留第i个位置右边的 N/2个最小需求之和

如何保留最小的需求之和呢,扫描两遍(左右),用优先队列保留N / 2个最小需求

最后只需要判断一下 left[i] + right[i] + 第i头牛的需求 <= F就可以了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 100010
typedef long long ll;

struct Cows{
    ll score, aid;
}cow[maxn];

int N, C;
ll F, left[maxn], right[maxn];

bool cmp(const Cows a, const Cows b) {
    return a.score < b.score;
}

void solve() {

    sort(cow, cow + C, cmp);
    for(int i = 0; i < C; i++)
        left[i] = right[i] = 0;
    priority_queue<ll> pq;

    ll sum1 = 0;
    for(int i = 0; i < C; i++) {

        if(i < N / 2) {
            pq.push(cow[i].aid);
            sum1 += cow[i].aid;
            continue;
        }
        left[i] = sum1;
        if(cow[i].aid >= pq.top())
            continue;
        sum1 -= pq.top();
        pq.pop();
        sum1 += cow[i].aid;
        pq.push(cow[i].aid);
    }

    while(!pq.empty())
        pq.pop();

    sum1 = 0;
    for(int i = C - 1; i >= 0; i--) {
        if(i > C - 1 - N / 2) {
            sum1 += cow[i].aid;
            pq.push(cow[i].aid);
            continue;
        }
        right[i] = sum1;
        if(cow[i].aid >= pq.top())
            continue;
        sum1 -= pq.top();
        pq.pop();
        sum1 += cow[i].aid;
        pq.push(cow[i].aid);
    }

    ll ans = -1;
    for(int i = N / 2; i <= C - 1 - N / 2; i++)
        if(left[i] + right[i] + cow[i].aid <= F)
            ans = cow[i].score;
    printf("%lld\n", ans);
}

int main(){
    while(scanf("%d%d%lld",&N, &C, &F) != EOF) {
        for(int i = 0; i < C; i++)
            scanf("%lld%lld", &cow[i].score, &cow[i].aid);
        solve();
    }
    return 0;
}
时间: 2024-08-05 11:14:45

POJ - 2010 Moo University - Financial Aid 贪心+优先队列的相关文章

poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)

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 wish

POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在,则输出-1(一开始因为没看见这个,wa了几次). 这个题的第一种做法就是用两个优先队列+贪心. /* * Created: 2016年03月27日 14时41分47秒 星期日 * Author: Akrusher * */ #include <cstdio> #include <cstdl

poj -2010 Moo University - Financial Aid (优先队列)

http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U"大学学费非常昂贵,并非每一头小牛都能支付的起,很多小牛都需要经济援助,但是学校只有有限的资金F. "Moo U"大学只会从C个学生里选N个学生出来,(N是奇数),但是希望N头小牛CSAT得分的中位数越高越好.输入N,C,F 接下来C行,每行包括一个得分和需要申请的经济援助,输出符合条件

POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆

考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O(1)找到最大/最小值,并能持续维护. 复杂度 push() = O(logn); pop() = O(logn); BinaryHeap() = O(nlogn); 实现 数组下标从1开始的情况下,有 Parent(i) = i >> 1 LChild(i) = i << 1 RChi

poj 2010 Moo University - Financial Aid 大顶堆维护最小和

题意: 有c有牛,从中选(n-1)/2头,使他们的得分中位数最大且需要的资金援助和不超过f. 分析: 堆的运用大顶堆维护最小和. 代码: //poj 2010 //sep9 #include <iostream> #include <queue> #include <algorithm> using namespace std; const int maxN=100024; int dpl[maxN],dpr[maxN]; priority_queue<int&g

POJ 2010 Moo University - Financial Aid 堆的高级应用 -- 维护最小(最大和)

题目大意:有N头牛,每头牛两个权值,A和B.从这N头牛中选取C头牛,使得: 1.这些牛中A权值的中位数尽量大. 2.这些牛的B权值的和小于题中所给的F 输出这个最大的A权值的中位数:如果没有满足题意的解,就输出-1.值. 思路: 堆有一个神奇的功能.假设上图是一个数组,在B从A到C移动的过程中,利用大根堆可以维护出B在所有位置时,从A到B中选K个值的和的最小值,并在nlogn内得到答案. 方法如下:先把[A,A + K]的元素加入到一个大根堆中,记录它们的总和.之后让B不断向后循环,把B加入到大

Moo University - Financial Aid (poj 2010 优先队列 或者 二分)

Language: Default Moo University - Financial Aid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5551   Accepted: 1663 Description Bessie noted that although humans have many universities they can attend, cows have none. To remedy this p

poj2010 Moo University - Financial Aid

Moo University - Financial Aid 题意: 一个私立学校的学生要申请奖学金,而学校的金额有限.因此,学校希望在金额不超过F的情况下从C中选得N对数. 给出三个数N,C,F.分别代表在C对数中要取得N对数. 而每对数分别代表成绩,跟申请金额.要求取得N对数中的总金额不超过F的条件下,然取得中间的以为学生的成绩最高.(N为even) 算法分析: 本题有两种解法,一种是用优先队列,一种是二分; 一.利用堆实现 先说堆的实现方法,我们能够现对头尾的N/2进行处理,由于头尾的N/

挑战程序设计竞赛2.4习题:Moo University - Financial Aid POJ - 2010

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