Moo University

来源:互联网 发布:淘宝转化率控制在多少 编辑:程序博客网 时间:2024/06/10 18:47

Problem C

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 2
Problem 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 <br> <br>* 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 <br>
 

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. <br>
 

Sample Input
3 5 7030 2550 2120 205 1835 30
 

Sample Output
35
 
题目大意:

美国新建立了一个大学,能够给N(奇数)个学生提供助学金,但是该学校有点穷,最多能提供助学金数额为F。现在总共有C个学生可待选择,给出了这些学生的成绩以及相应的助学金,然而学校希望这个N个学生的成绩的中位数尽可能地大,求这个中位数的最大值。

输入:第一行, N(1 <= N <= 19,999),C(N <= C <= 100,000),  F( 0 <= F <= 2,000,000,000);
            2...C+1行,每行两个数,score(1<=score<=2000000000),  aid(0 <= aid <=100,000)。
输出:可能的最大的中位数(成绩)。如果资金不足以承受这些学生则输出 -1。


直接优先队列开搞啊!!(

#include <iostream>#include <algorithm>#include <string.h>#include <stdio.h>#include <cmath>#include <queue>using namespace std;const int INF = 0x3fffffff;const int maxn = 100005;struct Node {    int left, right, score, calf;}node[maxn];bool cmp(const Node& n1, const Node& n2) {    return n1.score < n2.score;}int main() {        //freopen("in.txt", "r", stdin);    int n, c, f;    //cin >> n >> c >> f;    while (scanf("%d%d%d", &n, &c, &f) != EOF) {        for (int i = 1; i <= c; ++i) {            //cin >> node[i].score >> node[i].calf;            scanf("%d%d", &node[i].score, &node[i].calf);        }                sort(node + 1, node + c + 1, cmp);                //从左往右求left        int start = n / 2 + 1;        int end = c - start + 1;        int sum = 0;        priority_queue<int> que;        for (int i = 1; i < start; ++i) {            que.push(node[i].calf);            sum += node[i].calf;        }        for (int i = start; i <= end; ++i) {            node[i].left = sum;            if (node[i].calf < que.top()) {                int temp = que.top();                que.pop();                sum -= temp;                sum += node[i].calf;                que.push(node[i].calf);            }        }        while (!que.empty())            que.pop();        sum = 0;                for (int i = c; i > end; --i) {            que.push(node[i].calf);            sum += node[i].calf;        }        for (int i = end; i >= start; --i) {            node[i].right = sum;            if (node[i].calf < que.top()) {                int temp = que.top();                que.pop();                sum -= temp;                sum += node[i].calf;                que.push(node[i].calf);            }        }        int ans = -1;        for (int i = end; i >= start; --i) {            if (node[i].calf + node[i].left + node[i].right <= f) {                ans = node[i].score;                break;            }        }        printf("%d\n", ans);        //cout << ans << endl;    }}


原创粉丝点击