Hangzhou Electric 1009-fatmouse ' trade (greedy)

Source: Internet
Author: User
Tags time limit

Fatmouse ' tradeTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)Total submission (s): 54937 Accepted Submission (s): 18422

Problem 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 Amou NT 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

This is a simple greedy, first calculated that the transaction is the most worthwhile, the first to do that, until their own capital of 0


AC Code:


#include <cstdio>
#include <algorithm>
using namespace std;
struct node 
{
	int v,w;
	Double q;
} S[10000];
BOOL CMP (node X,node y)
{
	return x.q>y.q;
}
int main ()
{
	int i,n,m;
	Double a[10000];
	while (scanf ("%d%d", &m,&n), m!=-1| | N!=-1)
	{
		for (i=0;i<n;i++)
		{
			scanf ("%d%d", &S[I].V,&S[I].W);
			S[I].Q=S[I].V*1.0/S[I].W;
		}
		Sort (s,s+n,cmp);
		Double sum=0;
		for (i=0;i<n;i++)
		{
			if (s[i].w<=m)
			{
				sum+=s[i].v;
				M-=S[I].W;
			}
			else
			{
				sum+=m*s[i].q;
				break;
			}
		}
		printf ("%.3lf\n", sum);
	}
	return 0;
 




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.