hdu 2187 老人是真饿了(贪心)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 using namespace std;
5 struct node
6 {
7 int p,h;
8 }arr[1100];
9 int cmp(node x,node y)
10 {
11 return x.p<y.p;
12 }
13 int main()
14 {
15 int C;
16 scanf("%d",&C);
17 while(C--)
18 {
19 int n,m;
20 scanf("%d %d",&n,&m);
21 for(int i=1;i<=m;i++)
22 scanf("%d %d",&arr[i].p,&arr[i].h);
23 sort(arr+1,arr+1+m,cmp);
24 double ans=0;
25 for(int i=1;i<=m;i++)
26 {
27 if(n==0)
28 break;
29 if(arr[i].p*arr[i].h<=n)
30 {
31 ans+=arr[i].h;
32 n-=arr[i].p*arr[i].h;
33 }
34 else
35 {
36 ans+=n*1.0/arr[i].p;
37 n=0;
38 }
39 }
40 printf("%.2lf\n",ans);
41 }
42 system("pause");
43 return 0;
44
2 #include<cstdio>
3 #include<algorithm>
4 using namespace std;
5 struct node
6 {
7 int p,h;
8 }arr[1100];
9 int cmp(node x,node y)
10 {
11 return x.p<y.p;
12 }
13 int main()
14 {
15 int C;
16 scanf("%d",&C);
17 while(C--)
18 {
19 int n,m;
20 scanf("%d %d",&n,&m);
21 for(int i=1;i<=m;i++)
22 scanf("%d %d",&arr[i].p,&arr[i].h);
23 sort(arr+1,arr+1+m,cmp);
24 double ans=0;
25 for(int i=1;i<=m;i++)
26 {
27 if(n==0)
28 break;
29 if(arr[i].p*arr[i].h<=n)
30 {
31 ans+=arr[i].h;
32 n-=arr[i].p*arr[i].h;
33 }
34 else
35 {
36 ans+=n*1.0/arr[i].p;
37 n=0;
38 }
39 }
40 printf("%.2lf\n",ans);
41 }
42 system("pause");
43 return 0;
44