HDU1009 FatMouse' Trade(贪心)
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct node{ double j,f; double pr; }food[10100]; cmp cmp(node a,node b){ if(a.pr == b.pr) return a.f < b.f; return a.pr > b.pr; } int main(){ int n,m; while(scanf("%d%d",&m,&n) && n != -1 && m != -1){ for( int i = 0;i < n;i ++){ scanf("%lf%lf",&food[i].j,&food[i].f); food[i].pr = food[i].j / food[i].f ; } sort(food,food + n,cmp); double sum = 0; for(int i = 0;i < n;i ++){ if(m <= food[i].f ){ sum += food[i].j * (m / food[i].f); break; } if(m < 0){ break; } sum += food[i].j; m -= food[i].f; } printf("%.3lf\n",sum); } return 0; }