Title Source: http://acm.hdu.edu.cn/showproblem.php?pid=1009
The subject as a basic problem of greed, very simple, and I think the structure of the most intuitive, but in the time to do the problem should pay attention to 0 1 and 1 0 of these two sets of test data, otherwise will WR
Code:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct t{
int catfood,javabe;
Double UnitPrice;
} A[10005];
BOOL CMP (T a,t b) {
return a.unitprice>b.unitprice;
}
int main () {
int m,n;
while (~SCANF ("%d%d", &m,&n) &&! ( M==-1&&n==-1)) {
// if (m==-1| | N==-1) break;
Double sum=0;
if (n==0) printf ("%.3lf\n", 0);
else{
for (int i=0;i<n;i++) {
scanf ("%d%d", &a[i].javabe,&a[i].catfood);
A[i].unitprice= double (a[i].javabe)/a[i].catfood;
}
Sort (a,a+n,cmp);
for (int i=0;m>0;i++) {
if (a[i].catfood<=m)
Sum+=a[i].javabe,m-=a[i].catfood;
else
sum+= (double) m*a[i].unitprice,m=0;
}
printf ("%.3lf\n", sum);
}
}
return 0;
}