Moo University - Financial Aid
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4701 | Accepted: 1427 |
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.
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
* 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.
Source
题目大意:
美国新建立了一个大学,能够给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。
思路: 水题, 按照分数排序,优先队列预处理左右长度为n/2最小的cost, 然后从大到小枚举,找到 l[i] + r[i] + cost[i]的就直接break
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class Main
{
static final int maxn = 100007;
static int[] l = new int[maxn];
static int[] r = new int[maxn];
static Queue<Integer> q = new PriorityQueue<Integer>(1,new Comparator<Integer>() //这个1是设置初始大小,并没啥用,因为多了 他会扩充
{
public int compare(Integer a, Integer b) //这里是compare,不会自动提示
{
return b - a; 优先队列是反着的!!别忘记了!!
}
});
static class node implements Comparable<node> //要给一个类排序的时候 要有继承
{
int cost, score;
node(int ss, int cc) //构造函数
{
this.cost = cc;
this.score = ss;
}
public int compareTo(node o) //重载 compareTo
{
if(score != o.score)//这里必须要重构compareTo函数 否则上面的继承会报错 记住这里是compareTo
return score - o.score;
return cost - o.cost;
}
public String toString() //用来调试用的
{
return "score = " + score + ", cost = " + cost;
}
}
static node[] a = new node[maxn];
static Scanner in = new Scanner(System.in);
public static void main(String[] agrs)
{
while(in.hasNext())
{
int n = in.nextInt();
int c = in.nextInt();
int f = in.nextInt();
Arrays.fill(l,0);
Arrays.fill(r, 0);
q.clear();
for(int i = 1; i <= c; i++)
a[i] = new node(in.nextInt(), in.nextInt());
Arrays.sort(a, 1, 1+c);
//System.out.println(a[1].toString()); 调试
// System.out.println(a[1].score);
System.out.println(q.size());
int sum = 0;
for(int i = 1; i <= c; i++)
{
if(i <= n/2)
{
sum += a[i].cost;
q.add(a[i].cost);
}
else
{
l[i] = sum;
if(a[i].cost < q.element())
{
sum += a[i].cost;
sum -= q.element();
q.poll();
q.add(a[i].cost);
}
}
}
q.clear();
sum = 0;
for(int i = c; i >= 1; i--)
{
if(i > c-n/2)
{
sum += a[i].cost;
q.add(a[i].cost);
}
else
{
r[i] = sum;
if(a[i].cost < q.element())
{
sum += a[i].cost;
sum -= q.poll();
q.add(a[i].cost);
}
}
}
int ans = -1;
for(int i = c-n/2; i > n/2; i--)
{
if(l[i] + r[i] + a[i].cost <= f)
{
ans = a[i].score;
break;
}
}
System.out.println(ans);
}
}
}