Hdu1009_fatmouse 'trade [greedy] [water question]

Source: Internet
Author: User
Fatmouse 'trade

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/others) total submission (s): 44470 accepted submission (s): 14872
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

Zjcpc2004

There are n rooms, and each room contains the food that fatmouse prefers, but each room

All the food needs to be replaced with the appropriate cat food. Fatmouse has m-pound cat food, so it can be changed to a maximum

Less food.

Idea: greedy method. Use a struct to store the amount of food in each room and the amount of cat food required. Per unit price of food (

That is, the size of food/cat food), the minimum unit price of each purchase, know m pound cat food used up

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct warehouse{    double j;    double f;}a[1100];bool cmp(warehouse a,warehouse b){    return a.f/a.g < b.f/b.g;}int main(){    int N;    double M;    while(~scanf("%lf%d",&M,&N)&& (M!=-1||N!=-1))    {        memset(a,0,sizeof(a));        for(int i = 0; i < N; i++)        {            scanf("%lf%lf",&a[i].j,&a[i].f);        }        sort(a,a+N,cmp);        double sum = 0;        for(int i = 0; i < N; i++)        {            if(M <= 0.000001)                break;            if(M >= a[i].f)            {                sum += a[i].j;                M -= a[i].f;            }            else            {                sum += M*a[i].j/a[i].f;                M = 0;            }        }        printf("%.3lf\n",sum);    }    return 0;}


Hdu1009_fatmouse 'trade [greedy] [water question]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.