poj2010 (Moo university-financial Aid) Priority queue

Source: Internet
Author: User

Description

Bessie noted that although humans has many universities they can attend, cows has none. To remedy this problem, she and her fellow cows formed a new university called the University of Wisconsin-farmside, "Moo U "For short."

Wishing to admit dumber-than-average cows, the founders created an incredibly precise admission exam called the Cow Sc Holastic 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, the 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 (whos E Total money is F, 0 <= F <= 2,000,000,000).

Worse Still, Moo U only have classrooms for an odd number N (1 <= n <= 19,999) of the C (n <= C <= 100,000) cal The VES who has applied. Bessie wants to admit exactly N calves on order to maximize educational opportunity. She still wants the median CSAT score of the admitted calves to being as high as possible.

Recall that the median of a set of integers whose size was odd is the middle value when they was sorted. For example, the median of the set {3, 8, 9, 7, 5} was 7, as there is exactly, and the values above 7 and exactly Low it.

Given the score and required financial aid for each calf this applies, the total number of calves to accept, and the total Amount of money Bessie have 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 7030 2550 2120 205 1835 30

Sample Output

35

Hint

Sample Output:If Bessie accepts the calves with CSAT scores of 5, 35, and, the median is. The total financial aid required is + 70 + + = <=.

Test instructions: give you c cow and each cow iq, spend, to you choose N Cow to make this n cow iq of the most median, if the sum of spending can not exceed F


Puzzle: First the IQ of each cow from small to large sequencing, and then enumerate the median IQ, for some median I, left and right to take the smallest n/2 cow. How to take it? First Use priority queue pretreatment each cow and the left and right side N/2 cow to spend the minimum value.


#include <iostream> #include <sstream> #include <fstream> #include <string> #include <map > #include <vector> #include <list> #include <set> #include <stack> #include <queue># Include <deque> #include <algorithm> #include <functional> #include <iomanip> #include < limits> #include <new> #include <utility> #include <iterator> #include <cstdio> #include < cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime>using namespace Std;const int maxn = 100010;priority_queue<int> p, q;int N, C, F;pair<int, int> cow[maxn];int L[MAXN    ], R[maxn];int Main () {CIN >> n >> c >> F;    for (int i = 0; i < C; ++i) scanf ("%d%d", &cow[i].first, &cow[i].second);    Sort (cow, cow+c);    The minimum value int s = 0 that the left N/2 Cow spends when preprocessing with I as the median number;       for (int i = 0; i < C; ++i) {if (p.size () = = N/2) L[i] = s; P.push (Cow[i].second);        s + = Cow[i].second;            if (P.size () > N/2) {s-= p.top ();        P.pop ();    }}//preprocessing with I for the median when the right n/2 the minimum value of the cow s = 0;        for (int i = c-1; I >= 0; i.) {if (q.size () = = N/2) R[i] = s;        Q.push (Cow[i].second);        s + = Cow[i].second;            if (Q.size () > N/2) {s-= q.top ();        Q.pop (); }}//From large to small enumeration median, to satisfy test instructions on exit for (int i = C (n+1)/2; I >= n/2; i) {int sum = Cow[i].second + l[i] + r[        I];            if (sum <= f) {cout << cow[i].first << Endl;        return 0;    }} cout << "-1" << Endl; return 0;}


Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

poj2010 (Moo university-financial Aid) Priority queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.