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

来源:互联网 发布:邓肯生涯数据 编辑:程序博客网 时间:2024/06/06 09:24

题目链接:https://cn.vjudge.net/problem/POJ-2010

题目大意:要从C头牛里选N头,已知每头牛的成绩和培养费,求使这C头牛的总费用不超过F时,其成绩中位数的最大值。(N为奇数)

思路:将奶牛按成绩排序,然后从高到低枚举中位数,从比中位数低的和高的奶牛中各选C/2头花费最低的,第一个满足条件的就是。

#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<vector>#include<queue>#include<cmath>using namespace std;typedef long long ll;const int maxn = 100000 + 5;const ll INF = 2e9 + 10;int n, c;ll F, low[maxn], up[maxn];struct Stu {   int score, cost;   bool operator < (const Stu& rhs) const {      return score < rhs.score;   }}st[maxn];int main(){    cin >> n >> c >> F;    for(int i = 0; i < c; i++)      scanf("%d%d", &st[i].score, &st[i].cost);    sort(st, st+c);    int half = n/2;    {        ll total = 0;        priority_queue<ll> q;        for(int i = 0; i < c; i++) {            low[i] = (q.size() == half ? total : INF);            total += st[i].cost;            q.push(st[i].cost);            if(q.size() > half) {                total -= q.top(); q.pop();            }        }    }    {        ll total = 0;        priority_queue<ll> q;        for(int i = c-1; i >= 0; i--) {            up[i] = (q.size() == half ? total : INF);            total += st[i].cost;            q.push(st[i].cost);            if(q.size() > half) {                total -= q.top(); q.pop();            }        }    }    for(int i = c-1; i >= 0; i--) {        if(up[i] + (ll)st[i].cost + low[i] <= F) {            printf("%d\n", st[i].score);            return 0;        }    }    printf("-1\n");}



0 0