hdu 2187

xiaoxiao2021-02-28  111

Input 输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(0

题解:

贪心暴力之。。排序每次选取最便宜的即可。

代码:

#include <iostream> #include <algorithm> #include <cstdio> using namespace std; typedef struct { int price; int weight; }rice; rice r[1010]; bool cmp(rice a,rice b) { return a.price<b.price; } int main() { int T; cin>>T; while(T--) { int n,m; cin>>n>>m; for(int i=0;i<m;i++) scanf("%d%d",&r[i].price,&r[i].weight); sort(r,r+m,cmp); double total=0; for(int j=0;j<m&&n>0;j++) { while(r[j].weight&&n) { if(n<r[j].price) {total+=n*(double)1/(double)r[j].price;n=0;break;}; n-=r[j].price; total++; r[j].weight--; } } printf("%.2lf\n",total); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-59338.html

最新回复(0)