Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
Sample Output
13.333 31.500
翻译:
众所周知Sidney非常喜欢请ACM程序设计协会的小姐姐们吃烧烤。
这天晚上Sidney准备了 M 块钱,准备去菜市场给小姐姐们买烧烤。
菜市场周围有 N 家烧烤摊。第 i 家烧烤摊有 J[i] 串烧烤,总价为F[i] 元。由于Sidney和烧烤摊老板们很熟,他不需要买整数串烧烤,而是可以任意支付 F[i] * a% 元去得到 J[i] * a% 串烧烤。这里,a 表示0到100间的一个实数。
现在他将这项任务分配给了你:请告诉他,他最多能给小姐姐们买回多少烧烤。
Input
输入包含多组测试数据。
对于每组测试数据,以包含了两个非负整数 M 和 N 的一行开始。接下来的 N 行,每行相应包含了两个非负整数 J[i] 和 F[i]。
最后一组测试数据是两个 -1。所有的整数均不超过 1000。
Output
对于每组测试数据,在单独的一行中打印一个实数,精确到小数点后 3 位数,表示Sidney能买回的最多的烧烤数。
Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
思路: 挑性价比高的先买。
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct T{
double num;
double total;
double aver;
} a[10015];
bool cmp(T a,T b){
return a.aver<b.aver;
}
int main (){
int m,n;
while (cin>>m>>n){
if (m==-1&&n==-1)
break;
for(int i=0;i<n;i++){
cin>>a[i].num>>a[i].total;
a[i].aver =a[i].total/a[i].num;
}
sort(a,a+n,cmp);
double sum1=0,sum2=0;// 总个数 总价
int i;
for(i=0;i<n;i++){
if(m>=a[i].total ){
sum1+=a[i].num ;
m-=a[i].total ;
}else {
sum1+=m/a[i].aver ;
break;
}
}
printf("%.3lf\n",sum1);
}return 0;
}