您的位置:首页 > 其它

FatMouse' Trade 10

2015-11-25 20:15 246 查看
FatMouse'Trade
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 57005 Accepted Submission(s): 19093


Problem Description
FatMouse preparedM pounds of cat food, ready to trade with the cats guarding the warehousecontaining his favorite food, JavaBean.

The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans andrequires F[i] pounds of cat food. FatMouse does not have to trade for all theJavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if hepays F[i]* a% pounds of cat
food. Here a is a real number. Now he is assigningthis homework to you: tell him the maximum amount of JavaBeans he can obtain.
FatMouse准备M号斤猫粮,愿与守卫装着他最喜欢的食物,JavaBean的仓库猫进行交易。

仓库有N个房间。第i个室包含Ĵ[I]磅的JavaBeans并需要F [i]于磅的猫食。 FatMouse没有交易在房间里所有的JavaBeans的,相反,他可能会得到Ĵ[我]*百分比磅的JavaBeans,如果他支付F [I]*百分比磅的猫粮。这里是一个实数。现在他指派本次作业中你:告诉他的JavaBeans他能获得的最高金额。

FatMouse准备了M磅的Cat-Food,以便用来跟小Cat交换好吃的JavaBean。
现在有N个房间,第i个房间有J[i]磅的JavaBean,其交换的筹码是F[i]磅的Cat-Food。
当然,FatMouse还是有很大的选择权的,对任意一个房间,它可以只交换一部分的Cat-Food。
现要求FatMouse以怎样的策略才能获得最多的Cat-Food。
这也是一道简单 &
典型的贪心算法题,
这道贪心比HDOJ-ACM-1052-TianJi
— The Horse Racing:田忌赛马要简单许多,
它的整体思路就是以javaBean/catFood比为基准,大比值房间优先。

Input
The input consistsof multiple test cases. Each test case begins with a line containing twonon-negative integers M and N. Then N lines follow, each contains twonon-negative integers J[i] and F[i] respectively. The last test case isfollowed by
two -1's. All integers are not greater than 1000.
输入由多个测试用例。每个测试情况下开始与含有两个非负整数M和N.然后N行遵循一条线,每个包含两个非负整数Ĵ[i]和F [i]于分别。最后一个测试用例后面跟着两个-1的。所有的整数不大于1000。

Output
For each testcase, 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.
对于每个测试用例,在一行中打印一个实数精确到3位小数,这是JavaBeans的是FatMouse能获得的最高金额。

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
代码如下:
#include<stdio.h>
#include<string.h>
int J[1002], F[1002];
double JF[1002];
int main()
{
    int j, m, n, i;
    double sum, temp;
    while(scanf("%d%d", &m, &n) != EOF && m != -1 && n != -1)
    {
        memset(J, 0, sizeof(J));
        memset(F, 0, sizeof(F));
        memset(JF,0, sizeof(JF));
        for(i=1; i<=n; ++i)  //从1开始 
        {
            scanf("%d%d", &J[i], &F[i]);
            JF[i] = J[i]*1.0/F[i];
        }

        for(i=2; i<=n; ++i)
        {
            if(JF[i] >= JF[i-1])
            {
                JF[0] = JF[i];
                J[0] = J[i];
                F[0] = F[i];
                
                JF[i] = JF[i-1];
                J[i] = J[i-1];
                F[i] = F[i-1];                
                
                for(j=i-2; j>=0; --j)
                {
                    if(JF[0] >= JF[j])
                    {
                        JF[j+1] = JF[j];
                        J[j+1] = J[j];
                        F[j+1] = F[j];                        
                    }
                    else {
                            JF[j+1] = JF[0]; 
                            J[j+1] = J[0]; 
                            F[j+1] = F[0]; 
                            break;
                        }
                }
            }
         } 
                  
         sum = 0;
         temp = m;
         for(i=1; i<=n; ++i)
         {
             temp = temp - F[i];
             if(temp >= 0) sum = sum + 1.0*J[i];
             else {sum = sum + (temp + F[i])*JF[i]; break;} 
            
         }
         printf("%.3f\n", sum);
        
        
    }   
    return 0; 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: