Round 06 E-FatMouse' Trade

来源:互联网 发布:python自带函数 编辑:程序博客网 时间:2024/05/06 10:38

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 37 24 35 220 325 1824 1515 10-1 -1
 

Sample Output

13.33331.500

题意:每个房间可用F[i]javabean换J[i]个猫最爱的事物;可按百分比换,问最多能换到多少;

思路,把ave从大到小进行排列,按顺序取完就好;

#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cstdlib>#include<cmath>#include<queue>using namespace std;struct node{double get;double pay;double ave;}room[100005];bool cmp(node x,node y){return x.ave>y.ave;}int main(){//freopen("input.txt","r",stdin);int m,n;while(scanf("%d%d",&m,&n)){   if(m==-1&&n==-1) break;    double ans=0;for(int i=0;i<n;i++){scanf("%lf%lf",&room[i].get,&room[i].pay);//printf("a=%f  b=%f\n",room[i].a,room[i].b);//        if(room[i].pay==0){//        ans+=room[i].get;//        room[i].ave=0;//        room[i].pay=100000000;//        continue;//}room[i].ave=room[i].get/room[i].pay;}sort(room,room+n,cmp);for(int i=0;i<n;i++){if(room[i].pay<=m){ans+=room[i].get;m-=room[i].pay;}else{ans+=m*room[i].ave;break;}}printf("%.3lf\n",ans); }return 0;}//第一次,程序崩溃,scanf忘加&了// 第二次,45行处没加break // 第三次,WA了好多次,本以为是没有考虑pay=0的情况,加上后就AC了,但网上有份和我之前几乎一模一样//的代码,提交后也AC了,于是我把pay=0的情况注释掉,也AC了,真是迷之AC。。。 




0 0
原创粉丝点击