POJ2010 Moo University - Financial Aid

POJ2010 Moo University - Financial Aid

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

* 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

1
2
3
4
5
6
3 5 70
30 25
50 21
20 20
5 18
35 30

Sample Output

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

Answer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

typedef pair<int, int> pii;

pii c[100050];
long long l[100050], r[100050];
priority_queue<int> pq;

int main() {
int N, C, F;
scanf("%d %d %d", &N, &C, &F);
for (int i = 0; i < C; ++i) {
scanf("%d %d", &c[i].first, &c[i].second);
}
sort(c, c+C);
if (N ==1) {
for (int i = C-1; i >= 0; --i) {
if (c[i].second <= F) {
printf("%d\n", c[i].first);
return 0;
}
}
printf("-1\n");
return 0;
}

int tmp = 0;
for (int i = 0; i < N/2; ++i) {
pq.push(c[i].second);
tmp += c[i].second;
}
l[N/2] = tmp;

for (int i = N/2+1; i <= C - N/2-1; ++i) {
if (pq.top() <= c[i-1].second) {
l[i] = l[i-1];
} else {
l[i] = l[i-1] - pq.top() + c[i-1].second;
pq.pop();
pq.push(c[i-1].second);
}
}

while(!pq.empty()) {
pq.pop();
}

tmp = 0;
for (int i = C-1; i > C-N/2-1; --i) {
pq.push(c[i].second);
tmp += c[i].second;
}
r[C-N/2-1] = tmp;

for (int i = C-N/2-2; i >= N/2; --i) {
if (pq.top() <= c[i+1].second) {
r[i] = r[i+1];
} else {
r[i] = r[i+1] - pq.top() + c[i+1].second;
pq.pop();
pq.push(c[i+1].second);
}
}

int ans = -1;
for (int i = C-N/2-1; i >= N/2; --i) {
if (l[i]+r[i]+c[i].second <= F) {
ans = c[i].first;
break;
}
}
printf("%d\n", ans);
return 0;
}

Analysis

  1. 从申请者里录取n个,n为奇数,要求他们需要的开销和不超过预算,求被录取者的得分最大可能的中位数。
  2. 从大到小遍历中位数的可能,利用优先队列维护左右两侧的最小开销。
# POJ

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×