讲真,这么水的题,我都不怎么好意思扔到博客上来,但是没办法啊,我总得证明一下今天上午我不是在寝室里瞎玩浪费掉的……
思路就是,把米按单价从小到大排个序,便宜的买的越多越好,直到钱花光为止……我真的都不好意思说这是个贪心……有这么简单的贪心?????
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 struct Node{ 5 int p,w; 6 }node[1000+5]; 7 int n,m,t; 8 bool cmp(Node a,Node b){return a.p<b.p;} 9 int main() 10 { 11 for(scanf("%d",&t);t;t--) 12 { 13 scanf("%d%d",&n,&m); 14 for(int i=1;i<=m;i++) scanf("%d%d",&node[i].p,&node[i].w); 15 sort(node+1,node+m+1,cmp); 16 double weight=0; 17 for(int i=1;i<=m;i++) 18 { 19 if(n > node[i].w*node[i].p) 20 { 21 weight+=node[i].w; 22 n-=node[i].w*node[i].p; 23 } 24 else 25 { 26 weight+=n*1.0/node[i].p; 27 break; 28 } 29 } 30 printf("%.2f ",weight); 31 } 32 }