http://acm.hdu.edu.cn/showproblem.php?pid=2570

我真是二啊本来一道挺简单的贪心我愣是想用01背包做,做吧可是还没做出来,不知道哪位用01背包做的可以给我讲讲。从小到大排序知道出现大于w的跳出。。哎写的时候各种错误。。粗心啊。。

#include <cstdio>
#include <cstdlib>
#include <cstring>
int cmp(const void*a,const void *b)
{
    return *(int*)a-*(int*)b;
}
int main()
{
    //freopen("d.txt","r",stdin);
    int n,t,v,w,a[107],sum,V,i;
    scanf("%d",&t);
    while(t--)
    {
        sum=V=0;
        memset(a,0,sizeof(a));
        scanf("%d%d%d",&n,&v,&w);
        for(i=0;i<n;i++)
        scanf("%d",&a[i]);
        qsort(a,n,sizeof(a[0]),cmp);
        for(i=0;i<n;i++)
        {
           if(1.0*(sum+a[i])/(V+1)<=(w*1.0))
           {
               sum+=a[i];
               V+=1;
           }
           else
           break;
        }
        if(sum==0)
        printf("0 0.00\n");
        else
        printf("%d %.2lf\n",V*v,0.01*sum/V);
    }
    return 0;
}

  

相关文章:

  • 2022-03-08
  • 2022-01-11
  • 2021-06-25
  • 2021-10-17
  • 2021-09-04
  • 2021-08-31
  • 2021-09-16
  • 2022-12-23
猜你喜欢
  • 2021-09-18
  • 2021-03-29
  • 2022-12-23
  • 2021-08-05
  • 2021-04-09
  • 2021-06-25
相关资源
相似解决方案