Saving HDU (贪心)

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

好久不刷题,拿到水题切了切,,,,,题意刚开始都没有理解,,,,真是弱了,,,,

简单贪心,,,,注意分割后的价值和对应的体积成正比

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <ctype.h>
 7 #include <iomanip>
 8 #include <queue>
 9 #include <stdlib.h>
10 using namespace std;
11  
12 struct node{
13     int val,vol;
14 }s[100000];
15 bool cmp(node x,node y)
16 {
17     return x.val>y.val;
18 }
19 int main()
20 {
21     int m,n,i;
22     while(scanf("%d",&m),m)
23     {
24         scanf("%d",&n);
25         for(i=0;i<n;i++)
26         scanf("%d%d",&s[i].val,&s[i].vol);
27         sort(s,s+n,cmp);
28         int sum=0;
29         for(i=0;i<n;i++)
30         {
31             if(m>s[i].vol)
32             {
33                 sum+=s[i].val*s[i].vol;
34                 m-=s[i].vol;
35             }
36             else
37             {
38                 sum+=m*s[i].val;
39                 break;
40             }
41         }
42         printf("%d
",sum);
43     }
44     return 0;
45  } 
原文地址:https://www.cnblogs.com/wangmengmeng/p/5226851.html