poj 2976 Dropping tests (二分查找)
来源:互联网 发布:淘宝单坑产出什么意思 编辑:程序博客网 时间:2024/06/08 05:59
对于(a1+a2+....an)/(b1+b2........bn)=x,可以去掉其中k个数据 ,求x的最大值。
其实可以二分查找答案,我们只需要判断当平均分为x时,可不可行。
我们可以对上面的式子进行变形。。。。
a1+a2+.....an>=b1*x + b2*x +.......bn*x
继续变形。。。
(a1-b1*x)+(a2-b2*x)+.......(an-bn*x)>=0;
我们令c1=(a1-b1*x) c2=(a2-b2*x) ....cn=(an-bn*x)
对c1,c2,...cn进行排序,只取最大的n-k个之和,判断是否大于0。。。
PS:对于输出距离最近的整数,我们可以把答案加上0.5,然后取整(强制转化成int),就是答案,
比如83.3加上0.5=83.8 取整=83 比如是83.6 加上0.5=84.1 取整=84
#include<stdio.h>#define exp 1e-3#define N 1000+16int n,k;double c[N],a[N],b[N];void qsort(double a[],int l,int r)//快速排序{int i,j;double x;if(l<r){i=l;j=r;x=a[i];while(i<j){while(i<j&&a[j]<=x)j--;if(i<j)a[i++]=a[j];while(i<j&&a[i]>x) i++;if(i<j)a[j--]=a[i];}a[i]=x;qsort(a,l,i-1);qsort(a,i+1,r);}return ;}int judge(double x)//判断平均分为x时,可不可行{int i;double sum=0;for(i=0;i<n;i++){c[i]=100*a[i]-x*b[i];}qsort(c,0,n-1);for(i=0;i<n-k;i++){sum+=c[i];}return sum>=0;}int main(void){int i; double l,r,m;while(~scanf("%d%d",&n,&k)&&n+k!=0){ for(i=0;i<n;i++) scanf("%lf",&a[i]); for(i=0;i<n;i++) scanf("%lf",&b[i]); l=0;r=100; while(r-l>exp)//二分查找 { m=(l+r)/2; if(judge(m)) l=m; else r=m; } printf("%d\n",(int)(l+0.5));}}
0 0
- poj 2976 Dropping tests (二分查找)
- Dropping tests - POJ 2976 二分
- poj 2976 Dropping tests,二分
- poj 2976 Dropping tests (01规划,二分查找)
- POJ - 2976 Dropping tests(二分查找,最大化平均值)
- POJ 2976 Dropping tests(二分查找 最大化平均值)
- POJ 2976 - Dropping tests(二分搜索)
- POJ 2976 Dropping tests(贪心+二分)
- [POJ 2976] Dropping tests (二分答案)
- POJ 2976 Dropping tests (经典变形二分)
- Poj 2976 Dropping tests(二分)
- POJ 2976 Dropping tests 【二分:最大化平均值】
- 【POJ】2976 Dropping tests(二分)
- poj 2519 Dropping tests 二分
- poj 2976 Dropping tests
- POJ 2976 Dropping tests
- POJ 2976 Dropping tests
- poj 2976-Dropping tests
- TIANKENG’s restaurant【区间覆盖】
- 阶乘之和【DFS】【贪心】
- 想法结合运用
- Python轻松入门-30 多重继承
- Java之显示需要class,ifterface或者enum
- poj 2976 Dropping tests (二分查找)
- [BZOJ3527][Zjoi2014]力(FFT)
- c++注意事项
- JS For in and For (i=0;i<length;i++) different
- BZOJ 2946 [Poi2000]公共串 后缀自动机
- 背诵内容
- hdu 4722 Good Numbers(找规律)
- 问题整理
- win32编程中创建窗口时CW_USEDEFAULT的作用