HDU 1009 Greedy Basic problem

Source: Internet
Author: User

B-Greedy Foundation

crawling in process ... crawling failed time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u

Submit Status

Description

Fatmouse prepared M pounds of cat food, ready-to-trade with the cats guarding the warehouse containing he favorite food, JavaBean.
The warehouse has N rooms. The i-th contains j[i] pounds of JavaBeans and requires f[i] pounds of cat food. Fatmouse does not has the to trade for all the JavaBeans in the the the same, instead, he may get j[i]* a% of pounds JavaBeans if he Pays f[i]* a% pounds of cat food. Here A is a real number. Now he's 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 is begins with a line containing the non-negative integers M and N. Then N lines follow, each contains, non-negative integers j[i] and f[i] respectively. The last test case was followed by Two-1 ' s. All integers is 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 J Avabeans that Fatmouse can obtain.

Sample Input

5 37 24 35 220 325 1824 1515 10-1-1

Sample Output

13.33331.500 The main topic: mice bribe cats, to steal things, pay f[i] cat food can eat to j[i] JavaBean, at the same time every thing inside the warehouse is not necessarily all, you can want any percentage of things, at the same time pay the corresponding percentage of the price, Ask how to choose to make the mice eat the most javabean. Thinking Analysis: The first difference from knapsack problem, knapsack problem each item is not able to split, this is why knapsack problem can not be greedy to solve the reason, with greed to do this problem, it is clear that the least cost to get the most return on the strategy is we should choose, because you can choose any percentage, That is to say that the final money can be spent, the greedy strategy of this problem is to choose the most cost-effective items first purchase, that is, the Value/cost value is the largest, with this as the standard from the big to the small sort, each time to choose the most cost-effective warehouse, constitutes the correct answer. Code:#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
UsingNamespace Std;
struct Nod
{
Double C;
Double V;
Double S;
};
Constint MAXN=1000+10;
NodJava[MAXN];
boolCmp(NodA,NodB)
{
Return a. s>b. s;
}
IntMain()
{
int n, I;
Double mK;
While(scanf("%lf%d", &m, &n) && (M!=-1|| N!=-1))
{
Double K=0;
For(int I=0; I<n; I++)
{scanf("%lf%lf", &java[I].v, &java[I].c);
Java[I].s=java[I].v/java[I].c;
}
Sort(JavaJava+nCmp);
For(I=0; I<=n-1; I++)
{
If(M>=java[I].c)
{
M-=java[I].c;
K+=java[I].v;
}
Else
{
K+=(Double) m/java[i].c*java[i< Span class= "Sh-symbol" >].v              break          }
     Span class= "Sh-cbracket" >
     printf ( "%.3lf\n" , K     }
    return 0
/span>

HDU 1009 Greedy basic problem

Related Article

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.