Some projects--mourning the 512 Wenchuan earthquake--The old man is really hungry.

Source: Internet
Author: User

For survivors of the disaster, the most urgent is to solve the problem of food and clothing, the relief forces on the one side of the organization to get through the traffic, while organizing the procurement of grain. Now assume that a certain amount of relief funds to go to the market to buy rice (in bulk). If the market has m species of rice, the price and weight of various rice is known, ask, in order to meet the needs of more victims, the maximum amount of rice can be purchased?
Input data first contains a positive integer c, which indicates that there is a C set of test cases, the first line of each set of test cases is two integers n and m (0<n<=1000,0<m<=1000), respectively, the amount of funds and the type of rice, then the M row of data, Each line contains 2 integers p and h (1<=p<=25,1<=h<=100), each representing the unit price and the weight of the corresponding rice. Output for each set of test data, export the maximum weight that can be purchased for rice (you can assume that you are buying not just all of the rice).
The output of each instance occupies one row, leaving 2 decimal places. Sample Input
17 23 34 4
Sample Output
2.33

Idea: The unit price is sorted first, if the same row quantity, the total price of each kind of rice will be calculated, if less than the budget cycle continues, greater than the budget minus the excess portion, the end of the cycle.

Code

#include <iostream> #include <algorithm> #include <cstdio>using namespace std;struct intn{int p,h;} D[1100];bool CMP (INTN a,intn b) {if (A.P==B.P) return A.h>b.h;return A.P<B.P;} int main () {int c,n,m,i,money;double sum;while (cin>>c) {while (c--) {  cin>>n>>m;  for (i=0;i<m;i++)  cin>>d[i].p>>d[i].h;  Sort (d,d+m,cmp);  sum=0;  money=0;  for (i=0;i<m;i++)  {money+=d[i].p*d[i].h; sum+=d[i].h; if (money>n) {sum-= (money-n) * (double) 1/d[i].p;break ; }  }  printf ("%.2lf\n", Sum);}} return 0;}



Some projects--mourning the 512 Wenchuan earthquake--The old man is really hungry.

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.