Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4235 | Accepted: 1293 |
Description
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
* 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
Sample Input
3 5 70
30 25
50 21
20 20
5 18
35 30
Sample Output
35
Hint
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define maxn 100005 struct node {
int id,aid,sco;
}; int n,c,f;
node calve[maxn],s[maxn];
int sma[maxn],big[maxn]; bool cmp1(node a,node b) {
return a.aid < b.aid;
} bool cmp2(node a,node b) {
return a.sco < b.sco;
} int check(int x) {
int sum = s[x].aid,left = ,right = ; for(int i = ; i <= c; ++i) {
if(calve[i].id < x && sum + calve[i].aid <= f && left < n / ) {
sum += calve[i].aid;
++left;
} else if(calve[i].id > x && sum + calve[i].aid <= f && right < n / ) {
sum += calve[i].aid;
++right;
}
} if(left < n / ) return ;
if(right < n / ) return ;
return ; } void solve() {
int l = ,r = c; //for(int i = 1; i <= c; ++i) printf("%d ",s[i].sco);
while(l < r) {
int mid = (l + r + ) >> ;
if(check(mid) == ) {
r = mid - ;
} else if(check(mid) == ) {
l = mid + ;
} else {
l = mid;
}
} printf("%d\n",s[l].sco); } int main()
{ // freopen("sw.in","r",stdin); scanf("%d%d%d",&n,&c,&f); for(int i = ; i <= c; ++i) {
scanf("%d%d",&s[i].sco,&s[i].aid);
} sort(s + ,s + c + ,cmp2); for(int i = ; i <= c; ++i) {
calve[i].id = s[i].id = i;
calve[i].sco = s[i].sco;
calve[i].aid = s[i].aid;
} sort(calve + ,calve + + c,cmp1); int sum = ;
for(int i = ; i <= n; ++i) {
sum += calve[i].aid;
} if(sum > f) {
printf("-1\n");
} else {
solve();
} return ;
}
第二种方法是用大根堆,首先按照score从小到大排序。用大根堆预处理数组dpl[i]代表从1 到 i 能得到的数量为 n / 2的最小的aid ,dpr[i]代表 从i 到 c能得到的数量为 n / 2的最小的aid,具体方法是,若堆的大小小于 n / 2,则不断入列,否则进列,并删除堆顶。
则从大到小找出能满足条件的最大的中位数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; #define maxn 100005 struct node {
int sco,aid;
}; int n,c,f;
node s[maxn];
int dpl[maxn],dpr[maxn]; bool cmp(node a,node b) {
return a.sco < b.sco;
} bool cmp2(node a,node b) {
return a.aid < b.aid;
} void solve() {
priority_queue<int> q;
int sum = ;
for(int i = ; i <= c; ++i) {
if(q.size() < (n / )) {
sum += s[i].aid;
q.push(s[i].aid);
} else {
q.push(s[i].aid);
sum += s[i].aid;
sum -= q.top();
q.pop(); }
dpl[i] = sum;
} while(!q.empty()) q.pop();
sum = ;
for(int i = c; i >= ; --i) {
if(q.size() < (n / )) {
sum += s[i].aid;
q.push(s[i].aid);
} else {
q.push(s[i].aid);
sum += s[i].aid;
sum -= q.top();
q.pop(); }
dpr[i] = sum;
} int ans;
for(int i = c; i >= ; --i) {
if(i - < n / || c - i < n / ) continue;
if(dpl[i - ] + dpr[i + ] + s[i].aid <= f) {
ans = s[i].sco;
break;
}
} printf("%d\n",ans); } int main() {
//freopen("sw.in","r",stdin); scanf("%d%d%d",&n,&c,&f); for(int i = ; i <= c; ++i) {
scanf("%d%d",&s[i].sco,&s[i].aid);
} sort(s + ,s + c + ,cmp2);
int sum = ;
for(int i = ; i <= n; ++i) sum += s[i].aid;
if(sum > f) {
printf("-1\n");
return ;
} sort(s + ,s + c + ,cmp); solve(); return ; }