POJ-2010-Moo University - Financial Aid

题目描述:

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.

输入:

  • 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

输出:

  • Line 1: A single integer, the maximum median score that Bessie can achieve. If there is insufficient money to admit N calves,output -1.

输入示例:

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

输出示例:

1
35

提示:

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.

题目大意:

奶牛大学招收N名学生(奇数),有C名候选学生,该大学可以承受最多F的贷款,求能招收的学生中,中位数尽可能的大,如果没有满足条件的学生,输出-1。

思路1(优先队列):

以学生成绩排序,然后以每个学生为中位数,寻找比他成绩低的学生中尽可能贷款少的N/2个学生,和比他成绩高的学生中尽可能贷款少的N/2个学生。结果就是满足:

1
aid:cur_i+lower_i+upper_i <= F

中最大的那个。

难度就是如何保持尽可能少的贷款。

这里选择维持一个长度始终为N/2长度的优先队列。

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
/*
* @Date: 2019-10-06 17:36:17
* @LastEditors: BeckoninGshy
* @LastEditTime: 2019-10-07 16:04:57
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<utility>
using namespace std;
const int MAXN = 1e5+10;
const int INF = 0x3f3f3f3f;
typedef pair<int,int> PII;

PII arr[MAXN];
int M,N,S;
int lowa[MAXN],upa[MAXN];


int main(){
freopen("in.txt","r",stdin);
scanf("%d%d%d",&M,&N,&S);
for(int i = 0; i < N; i++){
scanf("%d%d",&arr[i].first,&arr[i].second);
}
int half = M/2;
sort(arr,arr+N);
memset(lowa,INF,sizeof(lowa));
memset(upa,INF,sizeof(upa));
priority_queue<int> q;
//确定以每个人为中位数, 比自己分数低的人的最小aid总和
int total = 0;
for(int i = 0; i < N; i++){
if(q.size() == half) lowa[i] = total;
total += arr[i].second;
q.push(arr[i].second);
if(q.size() > half){
total -= q.top();
q.pop();
}
}
while(q.size()) q.pop();
total = 0;
//确定以每个人为中位数, 比自己分数高的人的最小aid总和
for(int i = N-1; i >= 0; i--){
if(q.size() == half) upa[i] = total;
q.push(arr[i].second);
total += arr[i].second;
if(q.size() > half){
total -= q.top();
q.pop();
}
}
int ans = -1;
//从后往前寻找第一个满足条件的
for(int i = N-1; i >= 0; i--){
if(arr[i].second + lowa[i] + upa[i] <= S) {ans = arr[i].first;break;}
}
printf("%d\\n",ans);
return 0;
}

思路2(二分):

先对成绩排序,按成绩进行二分,对是否满足是中位数和sum <= F两个性质进行判断。

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
78
79
80
81
/*
* @Date: 2019-10-06 17:36:17
* @LastEditors: BeckoninGshy
* @LastEditTime: 2019-10-07 17:18:16
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<utility>
using namespace std;
const int MAXN = 1e5+10;
const int INF = 0x3f3f3f3f;
typedef pair<int,int> PII;

struct cow{
int csat,aid;
bool operator <(const cow &q)const{
if(csat == q.csat) return aid < q.aid;
return csat < q.csat;
}
}cows[MAXN];
struct n{
int index,aid;
bool operator <(const n &q)const{
return aid < q.aid;
}
}node[MAXN];
int M,N,S,half,ans = -1;
int aid[MAXN];

int check(int x){
int l = 0, r = 0, sum = cows[x].aid;
for(int i = 0; i < N; i++){
//在原位置的左边,且符合规定
if(l < half && node[i].index < x && sum + node[i].aid <= S){
l++;
sum += node[i].aid;
//在原位置的右边,且符合规定
}else if(r < half && node[i].index > x && sum + node[i].aid <= S){
r++;
sum += node[i].aid;
}
}
//都到不了half,说明无法满足小于等于S。
if(l < half && r < half) return -1;
if(l < half) return 1;
else if(r < half) return 0;
//l = r = half 找到了一个符合条件的,更新答案,继续寻找后面。
ans = cows[x].csat;
return 1;

}

int main(){
freopen("in.txt","r",stdin);
scanf("%d%d%d",&M,&N,&S);
for(int i = 0; i < N; i++){
scanf("%d%d",&cows[i].csat,&cows[i].aid);
}
half = M/2;
sort(cows,cows+N);//先按分数排序
for(int i = 0; i < N; i++){
node[i].index = i; //记录位置关系
node[i].aid = cows[i].aid;
}
sort(node,node+N); //排序是为了寻找尽可能小的aid
int l = 0, r = N-1;
while(l < r){
int mid = l+r+1>>1;
//试探中位数。
int f = check(mid);
if(f == 1) l = mid;
else if(f == 0) r = mid-1;
else break;
}
printf("%d\\n",ans);
return 0;
}