Moo University - Financial Aid
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6599   Accepted: 1926

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

3 5 70
30 25
50 21
20 20
5 18
35 30

Sample Output

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. 

Source

题意:给你c头牛,每买一头牛都有一定的花费,要求从中选出n头牛,使得在总花费<=f的情况下使得中位数最大;
错因分析:一开始就陷入了二分的固定思维,下面第一份的自己的WA代码
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
struct Node{
int s,m,id;
}node[20005];
int n,c,f,k;
bool cmp(Node a,Node b)
{
return a.m<b.m;
}
int ok(int mid)
{
int cnt1=0,cnt2=0,total=0;
for(int i=1;i<=c;i++)
if(node[i].s<mid&&cnt1<k)
{
cnt1++;
total+=node[i].m;
} //在加进一头牛时没有判断假设加进后总费用是否<=f;
else if(node[i].s>mid&&cnt2<(n-k))
{
cnt2++;
total+=node[i].m;
}
if(cnt1<k||cnt2<(n-k)||total>f)
return 0;
else return 1;
}
int main()
{
while(~scanf("%d %d %d",&n,&c,&f))
{
int maxn=0;
for(int i=1;i<=c;i++)
{
scanf("%d %d",&node[i].s,&node[i].m);
if(maxn<node[i].s)
maxn=node[i].s;
}
sort(node+1,node+1+c,cmp);
int l=0,r=maxn+1;
k=(n+1)/2;
while(r-l>1)
{
int mid=(l+r)/2;
if(ok(mid))
r=mid;
else
l=mid;
}
printf("%d\n",r-1);
}
return 0;
}

  第二份是AC代码:总的思想是先进行score排序,记录好数据后,二分枚举可能的取到的中位(因为score已经排好序,所以尽量往id大的取),然后money排序,进行贪心的选取。

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
struct Node{
int s,m,id;
}node[100005];
int n,c,f,k,score[100005],weight[100005];
bool cmpm(Node a,Node b)
{
return a.m<b.m;
}
bool cmps(Node a,Node b)
{
return a.s<b.s;
}
int main()
{
while(~scanf("%d %d %d",&n,&c,&f))
{
for(int i=1;i<=c;i++)
scanf("%d %d",&node[i].s,&node[i].m);
sort(node+1,node+1+c,cmps);
for(int i=1;i<=c;i++)
{
node[i].id=i;
score[i]=node[i].s;
weight[i]=node[i].m;
}
int ans=-1,l=0,r=c+1;
sort(node+1,node+1+c,cmpm);
k=n/2;
while(r-l>1)
{
int mid=(l+r)>>1;
int total=weight[mid],cnt1=0,cnt2=0;
for(int i=1;i<=c;i++)
if(cnt1<k&&node[i].id<mid&&node[i].m+total<=f)
{
cnt1++;
total+=node[i].m;
}
else if(cnt2<k&&node[i].id>mid&&node[i].m+total<=f)
{
cnt2++;
total+=node[i].m;
}
if(cnt1<k&&cnt2<k)
break;
else if(cnt1>=k&&cnt2>=k)
{
l=mid;//尽量往右选取
ans=score[l];
}
else if(cnt1<k)
l=mid;
else if(cnt2<k)
r=mid;
}
printf("%d\n",ans);
}
return 0;
}

  

最新文章

  1. php示例代码使用mysql_fetch_assoc函数
  2. leetcode 278. First Bad Version
  3. 关于each
  4. angular.element函数
  5. SQLite 命令
  6. 关于纯css布局的概况
  7. 分布式内存对象缓存系统Memcached-Linux下使用
  8. Android SDK Manager 更新代理配置 ,蛋碎了
  9. To enable integrated Windows authentication in Windows Vista/IIS 7
  10. 得到css style
  11. EXCEL数据导入数据库实例(NPOI)
  12. Redis集群之节点管理
  13. Easy UI下拉列表默认选中(多行)与为文本框赋值
  14. Android实现分享图片和文字的功能
  15. Run Keyword And Ignore Error,Run Keyword And Return Status,Run Keyword And Continue On Failure,Run Keyword And Expect Error,Wait Until Keyword Succeeds用法
  16. 2019_01_16 sem_init
  17. 【Java多线程】ReentrantReadWriteLock
  18. Tessaract 源码分析(转)
  19. C++函数调用时的参数传递-3中传递方式
  20. hadoop之 reduce个数控制

热门文章

  1. 小记---------spark优化之更优分配资源
  2. 【面试向】hihoCoder 1994 树与落叶
  3. sql实现同时向主表和子表插入数据方法
  4. AcWing175电路维修
  5. P3376 网络流-最大流模板题(Dinic+当前弧优化)
  6. 使用正则实现php的trim函数,支持全角空格
  7. Mac OS 下定制终端颜色
  8. (转载)sublime3安装markdown插件
  9. 机器学习-SVM-核函数
  10. O014、云计算与OpenStack