poj2976

来源:互联网 发布:ntfs for mac 14 编辑:程序博客网 时间:2024/05/21 02:32

//刚开始想按照每门课的分数除以总分的大小来排序,后来发现不对

//那么思路呢假设我们选了某个物品的集合,那么他们的的应该是集合里面的 总共获得的分数除以总分

//详细的一些东西我会在代码里面解释的;

#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;double y[1005];  int n,k;struct node{double a;double b;};node ave[1005];bool cmd(double x){int i;for(i=1;i<=n;i++)y[i]=ave[i].a-x*ave[i].b;//这个是文字中的一个变形sort(y+1,y+n+1);double sum=0;for(i=n;i>k;i--){sum+=y[i];}return sum>=0;}int main(){    while(scanf("%d%d",&n,&k)!=EOF,n+k)   {       int i;   for(i=1;i<=n;i++)   {   scanf("%lf",&ave[i].a);   }   for(i=1;i<=n;i++)   scanf("%lf",&ave[i].b);   double str=0;   double end=1;   for(i=1;i<=100;i++)//看到没有,这里用了二分搜索的办法,复杂度是nlogn   {   double mid=(str+end)/2;  // cout<<mid<<" ";   if(cmd(mid))   str=mid;   else   end=mid;   }   str*=100;   int str1=str;   if(str-str1>0.5)//关于题目要求的一个小处理   printf("%d\n",str1+1);   else   printf("%d\n",str1);   }}


0 0
原创粉丝点击