Fatmouse ' Trade

Source: Internet
Author: User

Title Source: http://acm.hdu.edu.cn/showproblem.php?pid=1009

The subject as a basic problem of greed, very simple, and I think the structure of the most intuitive, but in the time to do the problem should pay attention to 0 1 and 1 0 of these two sets of test data, otherwise will WR


Code:

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct t{
	int catfood,javabe;
	Double UnitPrice;
} A[10005];
BOOL CMP (T a,t b) {
	return a.unitprice>b.unitprice;
}
int main () {
	int m,n;
	while (~SCANF ("%d%d", &m,&n) &&! ( M==-1&&n==-1)) {
	//	if (m==-1| | N==-1) break;
		Double sum=0;
		if (n==0) printf ("%.3lf\n", 0);
		else{
	     	for (int i=0;i<n;i++) {
		    	scanf ("%d%d", &a[i].javabe,&a[i].catfood);
			    A[i].unitprice= double (a[i].javabe)/a[i].catfood;
	    	}
		   Sort (a,a+n,cmp);	
	    	for (int i=0;m>0;i++) {
            	if (a[i].catfood<=m)
        	     Sum+=a[i].javabe,m-=a[i].catfood;
            	else
        	    	sum+= (double) m*a[i].unitprice,m=0;	   
		    }
	     printf ("%.3lf\n", sum);
	  	}
	}
	return 0;
}




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.