• FatMouse' Trade http://acm.hdu.edu.cn/showproblem.php?pid=1009


    FatMouse' Trade

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34126    Accepted Submission(s): 11152

    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
     
    Author
    CHEN, Yue
     
    Source
     
    Recommend
    JGShining
     
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    typedef struct sss
    {
    	double x;
    	double y;
    	double z;
    }sss;
    struct sss a[1010];
    int cmp(const void *a,const void *b)
    {
    	sss *c=(sss *)a;
    	sss *d=(sss *)b;
    	return c->z>d->z?1:-1;
    
    }
    
    int main()
    {
    	int m,n;
    	while(scanf("%d %d",&m,&n),(n!=-1||m!=-1))
    	{
    		int i;
    		double toal=0;
    		for(i=0;i<n;i++)
    		{
    			scanf("%lf %lf",&a[i].x,&a[i].y);
    			a[i].z=a[i].x/a[i].y;
    		}
    		qsort(a,n,sizeof(a[0]),cmp);
    		for(i=n-1;i>=0;i--)
    		{
    
    			if(m<=a[i].y)
    			{
    				toal+=a[i].x*m/a[i].y;
    				break;
    			}
    			else
    			{
    				toal+=a[i].x;
    				m-=a[i].y;
    			}
    		}
    		printf("%.3lf
    ",toal);
    	}
    	return 0;
    }
    

    先理解一下题意:老鼠和猫交易,规则很简单,每个仓库里有J[i]磅鼠食,如果全部换走这些,老鼠需要F[i]磅猫食,当然老鼠也不用把一个仓库里的鼠食全部换走,可以按比例来换,比如

    5 3
    7 2
    4 3
    5 2
    意思就是老鼠有5磅猫食,先用2磅猫食换7磅鼠食(7 2),然后还剩3磅猫食,再用2磅猫食换5磅鼠食(5 2),还剩1磅,然后换1.33磅(4*1/3),所以一共最多换13.333磅鼠食。
    理解了意思就好做了,一看就是贪心算法,肯定要排序。排序时要注意,输入的J[i]F[i]一定用double不然的话计算结果double z=J[i]/f[i]是整型,我的就是那种情况,这和书上讲的不太一样,好像用int型时它只是把结果转换成了double型。
  • 相关阅读:
    HTTP 方法
    Nodejs 使用 bcrypt 库加密和验证密码
    Flutter 获取本地图片并剪切
    Flutter: 矩形边框
    [bzoj1385]Division expression
    项目中常用方法总结(将将DataTable数据集映射到实体对象)【转】
    hdu1010:Tempter of the Bone(dfs)
    hdu1010:Tempter of the Bone(dfs)
    BZOJ 2820 YY的GCD
    算法的封装与切换—策略模式(四)
  • 原文地址:https://www.cnblogs.com/wangyouxuan/p/3265839.html
Copyright © 2020-2023  润新知