• hdu_2111_Saving HDU(贪心)


    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2111

    题意:给你n个物品的单位体积价值和体积,求装满容量v的背包的最大价值。

    题解:乍一看还以为是背包问题,结果给的是单位体积的价值,一个简单的贪心就能水过

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int v,n,i,ans;
     5 struct node{
     6     int a,b;
     7     bool operator<(const node& b)const{return a>b.a;}
     8 }im[110];
     9 int main(){
    10     while(~scanf("%d",&v),v){
    11         scanf("%d",&n);
    12         for(i=1;i<=n;i++)scanf("%d%d",&im[i].a,&im[i].b);
    13         sort(im+1,im+1+n);
    14         for(i=1,ans=0;i<=n&&v>0;i++)
    15         if(v>=im[i].b)v-=im[i].b,ans+=im[i].b*im[i].a;
    16         else ans+=im[i].a*v,v=0;
    17         printf("%d
    ",ans);
    18     }
    19     return 0;
    20 }
    View Code



  • 相关阅读:
    linux shell执行远程计算机上的命令或者脚本(ssh)
    人到中年,愿我们的人生无悔
    资料
    新博客
    移植ok6410
    pm剩余要看的内容
    kernel boot
    regulator
    pm
    bochs安装一系列问题
  • 原文地址:https://www.cnblogs.com/bin-gege/p/5696180.html
Copyright © 2020-2023  润新知