Saving HDU HDU--2111 Greedy Algorithm + Fast Row

Topic link

By the way, last time when Haidong Group was facing internal and external difficulties, only the XHD couple were the only veterans of the company. Obviously, as a businessman who has worked hard for many years, XHD will not sit still.

One day, when he was thinking about a good solution to solve the problem, he suddenly thought of his family heirloom. It was a kit from his father as a gift when the company was founded. Xu's father explained at the time that he should not open it when it is not a last resort. "Isn't it the time when you need it most?" While thinking, XHD found this carefully kept kit and opened it. There was only one sentence in it, "There are treasures in the Qianrendong Cave at the northern foot of Hangzhou".

Apart from anything else, XHD picked up a big pocket and set off. He knew this cave of a thousand people. When he was young, his father used to take him to this hidden intersection and told him that it was a cave of thousand people. He now understands the meaning of his father's words.

Despite the impression, XHD took a lot of effort to find this unusually concealed hole. When he walked in, he was almost stunned, really dazzled! However, although there are many types of babies, the quantity of each type is not much. Of course, the price per unit volume of each type is different. In order to save the HDU, please help us to calculate as soon as possible how much value XHD can bring back. baby? (Assuming that the baby can be divided, the value after the division is proportional to the corresponding volume)**

Input

The input contains multiple test instances. The first line of each instance contains two integers v and n (v,n<100), which represent the pocket capacity and the type of baby, respectively. Each of the next n lines contains 2 integers pi And mi(0<pi,mi<10), respectively represent the unit price and corresponding volume of a certain baby, and the input ends when v is 0.

Output

For each test instance, please output the maximum value of the treasure XHD can retrieve. The output of each instance occupies one line.

Sample Input

2 2
3 1
2 3
0

Sample Output

5
With the help of tips, will HDU escape the crisis?
If you want to know what is going on, let’s listen to the next decomposition——

After solving this problem (note that the first parameter is v and the second parameter is n), the input parameters should be sorted in descending order according to the value, which means that we give priority to the expensive ones.
We store the baby’s information through the structure. In the quick sort function, we sort according to the price of the structure. The structure is packed and sorted in the sort, that is, all the data inside will be transferred to another location. Before sorting, we need to write a sorting function cmp. To use the sort function, you need to add a header file #include<algorithm>. The sort function we use in this question has three parameters, the structure name plus the structure subscript stuthat starts to participate in the sorting , the structure name plus the number of structures participating in the sorting stu+n, the sorting function cmp, fast The sorting function is sorted in ascending order by default. In this question, we need to write a function cmp in descending order of the baby price in the structure.

code show as below:

#include<iostream>
#include<algorithm> 
using namespace std;
struct muban
{
    
    
 	int p;
 	int m;
}stu[1000];
bool cmp(muban a,muban b)
{
    
    
 	return a.p>b.p;
}
int main()
{
    
    
 	int n,v;
 	while(cin>>v&&v!=0)
 	{
    
    
 		cin>>n;
  		int sum=0;
  		for(int i=0;i<n;i++)
  		{
    
    
   			cin>>stu[i].p>>stu[i].m;
  		}
  		sort(stu,stu+n,cmp);
  		for(int i=0;i<n;i++)
  		{
    
    
   			if(stu[i].m<v)
   			{
    
    
    				sum+=stu[i].m*stu[i].p;
    				v-=stu[i].m;
   			}
   			else if(stu[i].m>=v)
   			{
    
    
    				sum+=stu[i].p*v;
    				break;
   			}
  		}
  		cout<<sum<<endl;
 	}
 	return 0;
} 

Guess you like

Origin blog.csdn.net/Huo6666/article/details/107225073