Challenges Programming Contest 2.4 Exercise: 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 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.
Note that point, because the fractional interval is too large, there is no way dp achieve, can only start from a small number of number of grade.
Think of the requirements of the median, median hard to find, I do not know, but we know there is an intermediate value range, let's follow ranking score .
Because n / 2 before the median number, then there are n / 2 number, through all possible as a direct median, minimum before determined n / 2 and a minimum and the n / 2 and both plus the value of the median is less than f, then it is possible to select the answer that is possible to select among the largest of the median.
Ever since we know that the post-median sort cows [n / 2] ~ cows [c - n / 2] within the range (from 0 to the array of n-1 ends), calculated in each case before and after selecting n / 2 are selected such that a front the n / 2 and a minimum.
If the median Cows [n / 2] (i.e. the smallest possible range of median) prior to median then a total number of n / 2, until all the selected median n / 2 number will only It could be put before the Select All, and for them and. If later a median (i.e., the second smallest selectable range), then the median before a total of n / 2 + 1 number, where we choose to n / 2 such that the number and minimum number, we the answer can be deduced from a state - because the state is a full election, this time we will be more than a number of the remaining, we certainly need to be funded up to the rest of the cows, we are more than the last time new cows [n / 2] cows spend before the n / 2 requires maximum funding costs cows were compared to see which big. If the former is big, we have added other end cows left unchecked, because it cost more than cows [0] ~ cows [n / 2-1] in any cow tariff should be large; if the latter large then the state prior to the n / 2 requires maximum funding cows do not, we replaced a few cows [n / 2] the new cows can be selected so that the front and at least n / 2, in order analogy, every time the selected find the n / 2 of the maximum comparison with the new replacement, in order to save time and unnecessary expense, before the state can be saved with a data structure, in order to facilitate maximum is found , we use the priority queue for maintenance.
AC Code:
#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
struct Cow{
	int score;
	int need;
	friend bool operator<(Cow x, Cow y)
	{
		return x.score < y.score;
	} 
} Cows [100005]; // record the status of cows, score score, need required amount of subsidy
priority_queue <int> pq_pre, pq_pos; // pq_pre: reap the n bits before / 2 number, so that the amount of subsidy and minimum priority queue, pq_pos taken therefrom after n bits / number 2
int pre [100005]; // time in the median i, taken from the previous i n / 2 number, so that the minimum value and the amount of subsidy
int pos[100005];
int total, ans = -1; // if the answer does not exist is -1, total is an intermediate variable, calculated or pre pos of
int cow_num; // pure counting function
int main(void)
{
	int n, c, f;
	scanf("%d %d %d", &n, &c, &f);
	for(int i = 0; i < c; i++)
	{
		scanf("%d %d", &cows[i].score, &cows[i].need);	
	}
	sort (cows, cows + c); // Sort score
	for (int i = 0; i <n / 2; i ++) // Initialize the front when the n / 2 are combined, which is the number of bits from cows [n / 2] .need front and n / 2 of minimum
	{
		total += cows[i].need;
		pq_pre.push (cows [i] .need); // remember also into the front of the queue
	}
	for(int i = n / 2; i < c - n / 2; i++)
	{
		pre[i] = total;
		if (! pq_pre.empty () && cows [i] .need <pq_pre.top ()) // if the queue is non-empty (c == 1 is prevented) and new funding cows than the maximum queue cows funding to small, then delete the queue before it the largest, put the new head of the cow
		{
			total -= pq_pre.top();
			pq_pre.pop();
			total += cows[i].need;
			pq_pre.push(cows[i].need);
		}
	}
	total = 0;
	for (int i = c - 1; i> = c - n / 2; i -) // Similarly calculated median among cows after [c - - n / 2 1] when n / 2 head
	{
		total += cows[i].need;
		pq_pos.push(cows[i].need);
	}
	for(int i = c - n / 2 - 1; i >= n / 2; i--)
	{
		pos[i] = total;
		if(!pq_pos.empty() && cows[i].need < pq_pos.top())
		{
			total -= pq_pos.top();
			pq_pos.pop ();
			total += cows[i].need;
			pq_pos.push(cows[i].need);
		}
	}
	for (int i = c - n / 2 - 1; i> = n / 2; i -) // iterate descending, first, subject to the proviso answer is found
		if(pre[i] + pos[i] + cows[i].need <= f)
		{
			ans = cows[i].score;
			break;
		}
	printf("%d\n", ans);
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/jacobfun/p/12244509.html