杭电 1009(贪心算法)

<span style="font-size:18px;"></span><h1 style="text-align: center; color: rgb(26, 92, 200);">FatMouse' Trade</h1><span size="+0"></span><div style="text-align: center;"><span style="color: green; font-family: Arial; font-size: 12px; "><strong>Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)</strong></span></div><strong></strong><div style="text-align: center;"><span style="color: green; font-family: Arial; font-size: 12px;">Total Submission(s): 43192    Accepted Submission(s): 14432</span></div>

<div class="panel_title" align="left">Problem Description</div><div class="panel_content">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.
</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Input</div><div class="panel_content">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.
</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Output</div><div class="panel_content">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.
</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Sample Input</div><div class="panel_content"><pre><div style="font-family: 'Courier New', Courier, monospace;">5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1</div>

Sample Output

13.333
31.500

Author

CHEN, Yue

Source

ZJCPC2004

思路:直白的贪心算法。


<span style="font-size:18px;">题意:大老鼠,想通过猫粮与猫换取他最喜欢的Java豆,求解所能换取到的Java豆的最大值。</span>
<span style="font-size:18px;">题中的m可以看成钱数,n是仓库数。下面的n行 输入的两个数分别可以看成是数量,需要的金钱数。</span>
<span style="font-size:18px;">代码如下:</span>
<span style="font-size:18px;">#include<stdio.h>
#include<algorithm>
using namespace std;
struct sum{
    double a;
    double b;
    double c;
}s[1010];
int cmp(sum x,sum y)
{
    return x.c>y.c;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&m,&n),!(m==-1&&n==-1))
    {
        int i;
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf",&s[i].a,&s[i].b);
            s[i].c=s[i].a/s[i].b;
        }
        sort(s,s+n,cmp);
        double sum1=0;
        for(i=0;i<n;i++)
        {
            if(m*s[i].c<s[i].a)
            {
                sum1+=m*s[i].c;
                break;
            }
            else
            {
                sum1+=s[i].a;
            }
                m-=s[i].b;

        }
        printf("%0.3lf\n",sum1);

    }
    return 0;
}</span>
<span style="font-size:18px;">
</span>

杭电 1009(贪心算法)

时间: 2024-10-07 04:39:40

杭电 1009(贪心算法)的相关文章

FatMouse&#39; Trade(杭电1009)

FatMouse' Trade Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 1   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description FatMouse prepared M pou

杭电1009(FatMouse&#39; Trade)

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 51740    Accepted Submission(s): 17347 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gu

杭电 1009 FatMouse&#39; Trade

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 40975    Accepted Submission(s): 13563 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

杭电oj 1009 FatMouse&#39; Trade

Tips:本题采用贪心算法,类似于背包问题,关键在于读入数据之后,将数据按 J[i]/F[i] 从大到小排列即可. 1 /**本程序主要采用贪心算法思想,类似于背包问题*/ 2 #include<stdio.h> 3 #include<string.h> 4 int main() 5 { 6 int M,N; 7 while(scanf("%d %d",&M,&N)) 8 { 9 if(M == -1 && N == -1) 10

杭电1162--Eddy&#39;s picture(Prim()算法)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8070    Accepted Submission(s): 4084 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to b

HDU 5742 It&#39;s All In The Mind (贪心) 2016杭电多校联合第二场

题目:传送门. 题意:求题目中的公式的最大值,且满足题目中的三个条件. 题解:前两个数越大越好. #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int gcd(int a,int b) { if(!b) return a; return gcd(b,a%b); } int main() { int t; ci

杭电1050(贪心)

题目: The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform

HDU 1009 FatMouse&#39; Trade (贪心算法)

题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换,如果不能, 肯定是最后一次了,就把剩下全部换了,看看能换多少.求和. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <v

HDU 4864 Task (贪心+STL多集(二分)+邻接表存储)(杭电多校训练赛第一场1004)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4864 解题报告:有n台机器用来完成m个任务,每个任务有一个难度值和一个需要完成的时间,每台机器有一个可以工作的最长时间和一个可以完成的任务的难度的最大值, 一台机器能完成一个任务的条件是这台机器的最长工作时间和能完成任务的难度值必须都大于等于这个任务,而且一台机器最多完成一个任务,假设一个任务的时间为t,难度值为x,那么完成这个任务可以赚到的钱 money = 500 * t + 2 * x; 现在