Moo university-financial Aid (POJ 2010 Priority queue or two points)

Source: Internet
Author: User
Tags acos cmath

Language:DefaultMoo university-financial Aid
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5551 Accepted: 1663

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 + + = <=.

Source

Usaco 2004 March Green


Test instructions: C has a cow in the N-head, give their scores scores and aid aid, if the total funding of the N cattle is not more than F, but also the median of the largest number of points. To find the largest median.
Idea: According to the Order of fractions, enumerate each cow as the median, calculate the minimum subsidy for N/2 cows in front of the cow I and the minimum subsidy for the rear N/2 cattle (used to the priority queue). Finally, from the rear to find the first to meet the l[i]+r[i]+cow[i].second<=f is the answer.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 100005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO  N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using Nam Espace Std;priority_queue<int>q;int L[maxn],r[MAXN];p air<int,int> cow[maxn];int n,c,f;int main () {int i,j;        while (~SFFF (n,c,f)) {FRL (i,0,c) SFF (Cow[i].first,cow[i].second);        Sort (cow,cow+c);        int HALF=N/2;        int sum=0;            FRL (i,0,c) {if (Q.size () ==half) l[i]=sum;            else L[i]=inf;            Sum+=cow[i].second;            Q.push (Cow[i].second);                if (Q.size () >half) {sum-=q.top ();            Q.pop ();        }} sum=0; while (!        Q.empty ()) Q.pop ();            Free (i,c-1,0) {if (Q.size () ==half) r[i]=sum;            else R[i]=inf;            Sum+=cow[i].second;            Q.push (Cow[i].second);                if (Q.size () >half) {sum-=q.top ();            Q.pop ();        }} (i,c-1,0) {if (l[i]+cow[i].second+r[i]<=f) break; }       if (i>=0) pf ("%d\n", Cow[i].first);    Else PF (" -1\n"); } return 0;}


The second method uses two points:

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 100005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO  N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using Nam    Espace std;struct cow{int s,f; int id;} cow_s[Maxn],cow_f[maxn];int n,f,c,half;int cmp_s (cow A,cow b) {return A.S&LT;B.S;} int Cmp_f (Cow A,cow b) {return A.F&LT;B.F;}    int ok (int mid) {int I,ans;    int l=0,r=0,sum=cow_s[mid].f;             FRL (i,0,c) {if (l


Moo university-financial Aid (POJ 2010 Priority queue or two points)

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.