标签:
1 3 15 5 10 2 8 3 9
65
这个题显然是用贪心算法,价值高的取完再去取价值低的;
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 struct as 7 { 8 int v; 9 int w; 10 }aa[11]; 11 12 13 bool cmp(as x,as y) 14 { 15 return x.v>y.v;//按价值从高到底排序 16 } 17 int main() 18 { 19 int n,s,m,i; 20 scanf("%d",&n); 21 while(n--) 22 { 23 scanf("%d%d",&s,&m); 24 for(i=0;i<s;i++) 25 scanf("%d%d",&aa[i].v,&aa[i].w); 26 sort(aa,aa+s,cmp); 27 int sum=0; 28 for(i=0;i<s;i++) 29 { 30 if(m>=aa[i].w) 31 { 32 sum+=aa[i].v*aa[i].w; 33 m-=aa[i].w; 34 } 35 else 36 { 37 sum+=m*aa[i].v; 38 break; 39 } 40 } 41 printf("%d\n",sum); 42 } 43 return 0; 44 }
标签:
原文地址:http://www.cnblogs.com/Eric-keke/p/4696643.html