Greedy (6) Other greedy problems

table of Contents

One, other greedy problems

Second, OJ actual combat

CSU 1410: integer conversion

CSU 1588: Consolidated Fruit

HDU-1257 minimum interception system

HDU 5835 Danganronpa


One, other greedy problems

Except for the greedy problems of sorting, selection and knapsack, the others are summarized in this article.

 

Second, OJ actual combat

CSU 1410: integer conversion

topic:

Description

We can  convert an integer  A into another integer B by adding 1 or multiplying it by 2   .

Given two integers  X  ,   the Y-  , in order to calculate how many steps need to be at least  X  converted to  the Y-  . .

Input

The first line of input contains an integer  T   (1 ≤   T   ≤ 5 00 ), indicating that there are  T  groups of test data.

Each group of test data occupies one line and contains two integers  X  ,   Y   (1 ≤   X   ≤   Y   ≤ 10 18 ).

Output

For each test, at least how many steps can be output  X  is converted into  the Y  .

Sample Input

3
1 1
3 10
2 11

Sample Output

0
3
4

Code:

#include<iostream>
using namespace std;
 
int main()
{
	int t;
	long long x, y;
	cin >> t;
	while (t--)
	{
		cin >> x >> y;
		long long ans = 0;
		while (y > x)
		{
			if (y < x * 2)
			{
				ans += y - x;
				break;
			}
			if (y % 2 == 0)y /= 2;
			else y--;
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}

CSU 1588: Consolidated Fruit

topic:

Description

Now there are n piles of fruits, and the i-th pile has ai fruits. Now we need to merge these fruits into one pile, and the cost of each merger is the total number of fruits in the two piles. Find the minimum cost of combining all the fruits.

Input

The first line contains an integer T (T<=50), which represents the number of data groups.
The first row of each group of data contains an integer n (2<=n<=1000), which represents the number of piles of fruit.
The second line contains n positive integers ai (ai<=100), representing the number of fruits in each pile.

Output

Each group of data has only one row, which represents the minimum merge cost.

Sample Input

2
4
1 2 3 4
5
3 5 2 1 4

Sample Output

19
33

If only the adjacent ones can be merged, it is the interval DP. Refer to CSU-1592 Stone Merging  https://blog.csdn.net/nameofcsdn/article/details/112981922

This question is greedy, choose the smallest 2 merges each time, which is similar to Huffman coding

Code:

#include<iostream>
#include<queue>
using namespace std;
 
int main()
{
	int t, n, num;
	priority_queue<int>q;
	cin >> t;
	while (t--)
	{
		while (!q.empty())q.pop();
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin >> num;
			q.push(-num);
		}
		int ans = 0, a, b;
		while (q.size() > 1)
		{
			a = q.top();
			q.pop();
			b = q.top();
			q.pop();
			ans += a + b;
			q.push(a + b);
		}
		cout << -ans << endl;
	}
	return 0;
}

HDU-1257 minimum interception system

topic:

Description

A country has developed a missile interception system in order to defend against enemy missile attacks. However, this missile interception system has a flaw: although its first shell can reach any height, each subsequent shell cannot exceed the previous one. The height of the launch. One day, the radar caught an enemy missile attack. Since the system is still in the trial phase, there is only one system, so it may not be able to intercept all the missiles. 
What should I do? Build more systems! It's pretty easy for you to talk about. Cost? Cost is a big problem. So I came here for help. Please help calculate the minimum number of interception systems required. 

Input

Enter several sets of data. Each set of data includes: the total number of missiles (a positive integer), and the altitude at which the missile flies (the altitude data given by the radar is a positive integer not greater than 30,000, separated by spaces) 

Output

Corresponding to each set of data output to intercept all missiles at least how many sets of such missile interception systems are required. 

Sample Input

8 389 207 155 300 299 170 158 65

Sample Output

2

This is a question of greed.

Greedy strategy: Every time you choose a system to intercept shells, choose the system with the smallest height among all systems that can meet the conditions.

It means to minimize waste.

Code:

#include<iostream>
using namespace std;

int l[30000];

int main()
{
	int n, k, sum;
	while (cin >> n)
	{
		for (int i = 0; i < 30000; i++)l[i] = 30001;
		while (n--)
		{
			cin >> k;
			for (int i = 0; i < 30000; i++)if (l[i] >= k)
			{
				l[i] = k;
				break;
			}
		}
		sum = 0;
		for (int i = 0; i < 30000; i++)sum += (l[i] < 30001);
		cout << sum << endl;
	}
	return 0;
}

HDU 5835 Danganronpa

topic:

Description

Chisa Yukizome works as a teacher in the school. She prepares many gifts, which consist of   kinds with   quantities of each kind, for her students and wants to hold a class meeting. Because of the busy work, she gives her gifts to the monitor, Chiaki Nanami. Due to the strange design of the school, the students' desks are in a row. Chiaki Nanami wants to arrange gifts like this: 

1. Each table will be prepared for a mysterious gift and an ordinary gift. 

2. In order to reflect the Chisa Yukizome's generosity, the kinds of the ordinary gift on the adjacent table must be different. 

3. There are no limits for the mysterious gift. 

4. The gift must be placed continuously. 

She wants to know how many students can get gifts in accordance with her idea at most (Suppose the number of students are infinite). As the most important people of her, you are easy to solve it, aren't you?

Input

The first line of input contains an integer   indicating the number of test cases. 

Each case contains one integer  . The next line contains     numbers:  ,  .

Output

For each test case, output one line containing “Case #x: y” (without quotes) , where x is the test case number (starting from 1) and y is the answer of Chiaki Nanami's question.

Sample Input

1
2
3 2

Sample Output

Case #1: 2

The meaning of this question is to give a number of numbers, such as this question 2 numbers: 3,2

Each number represents the number of a different ball. Now I ask you how many balls can be selected at most so that they can be lined up in a row. Adjacent balls are different.

For example, in this question, 3 a, 2 b can be arranged into ababa, the answer should be 5.

But this question has a limitation. I don't want to go into details. In short, if this calculation exceeds half of sum, then the answer is half of sum.

Sum is the sum of these numbers, and max is the maximum value of these numbers.

For a number of general numbers, being able to line up as above is actually equivalent to max<=sum-max+1

In other words, for 2,3,6, max=6, sum=11, which just meets max=sum-max+1, then 2 a, 3 b, and 6 c can be arranged into cacacbcbcbc

For 2,3,7, max=7,sum=12, and max<=sum-max+1 is not satisfied, then no matter how they are arranged, they do not meet the conditions.

So now is the requirement, for the number of input, each number can become another integer that does not exceed it,

Finally, it is necessary to satisfy max<=sum-max+1, and find the maximum value of sum.

In fact, this problem is easy to solve and can be understood as greedy (really greedy enough)

First, sort, increase, and then all the previous numbers are unchanged, and the last number should be as large as possible when max<=sum-max+1.

That's it, no, yes, no.

Code:

#include<iostream>
#include<algorithm>
using namespace std;

int list[10];

int main()
{
    int t, n, sum, s;
    cin >> t;
    for (int i = 1; i <= t;i++)
    {
        cin >> n;
        sum = 0;
        for (int i = 0; i < n; i++)
        {
            cin >> list[i];
            sum += list[i];
        }
        sum /= 2;
        sort(list, list + n);
        s = 0;
        for (int i = 0; i < n - 1; i++)s += list[i];
        if (list[n-1] <= s + 1)s += list[n-1];
        else s += s + 1;
        if (s>sum)s = sum;
        printf("Case #%d: %d\n", i, s);
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/nameofcsdn/article/details/112754030