Greedy (4) Selection order problem

table of Contents

One, select the problem

Second, the selection of the set

CSU 1012: Prestige

CSU 1043: Kegmore

Three, the choice of deque

OpenJ_Bailian - 3377 Best Cow Line, Gold


One, select the problem

According to the few questions I have done, I divided the selection problems into set selection problems and double-ended queue selection problems.

 

Second, the selection of the set

CSU 1012: Prestige

topic:

Description

Are you watching closely?

Every great magic trick consists of three acts. The first act is called The Pledge, the magician shows you something ordinary, but of course, it probably isn't. The second act is called The Turn. The magician makes his ordinary something do something extraordinary. Now, if you're looking for the secret,you won't find it. That's why there's a third act, called The Prestige. This is the part with the twists and turns, where lives hang in the balance, and you see something shocking you've never seen before.

Li Lei and Han Meimei are magicians and they want to act the prestige. So they need to divide the props for the magic show. They have decided upon the following procedure: they choose props one by one, in turn, until all the props are chosen.

Li Lei and Han Meimei have different strategies in deciding what to choose. When faced with a choice, Li Lei always selects the prop that is most valuable to him. In case of a tie, he is very considerate and picks the one that is least valuable to Han Meimei. (Since Li Lei and Han Meimei are good friends, they know exactly how much value the other places on each prop.)

Han Meimei’s strategy, however, consists of maximizing her own final value(The total value of all the props she picked). She is also very considerate, so if multiple choices lead to the same optimal result, she prefers Li Lei to have as much final value as possible.

You are given the result that who chooses first. After Li Lei and Han Meimei have finished dividing all the props between themselves, what is the total value of the props each of them ends up with?

Input

On the first line a positive integer: the number of test cases, at most 100. After that per test case:

  • One line with an integer n(1≤n≤1,000): the number of props.

  • One line with a letter, either “L” for Li Lei or “H” for Han Meimei: the person that chooses first.

  • n lines with two integers li and hi (0≤li, hi≤1000) each: The values that Li Lei and Han Meimei assign to the i-th prop, respectively.

Output

For each test case, output one line with two integers: the value Li Lei gets and the value Han Meimei gets. Both values must be according to their own valuations.

Sample Input

3
4
L
100 80
70 80
50 80
30 50
4
L
10 1
1 10
6 6
4 4
7
H
4 1
3 1
2 1
1 1
1 2
1 3
1 4

Sample Output

170 130
14 16
9 10

 

Title:

There are n items, L and H take turns to fetch them

The value of each item to L is v1, and the value to H is v2

Enter who takes first, and the value of each item v1v2, and output the final result

L’s strategy is to take the item with the largest v1 every time. If there are multiple items, take the one with the smallest v2.

H’s strategy is to maximize the final total value as much as possible. If there are multiple options, choose the one with the smallest v1

Ideas:

Greedy, first sort all items according to L's strategy

Then H takes the item with the largest v2 each time, but to meet the restriction, that is, H can only take half of the previous items, because L is always taken backwards in this order

As long as this restriction is satisfied, H can get the optimal solution

Code:

#include<iostream>
#include<algorithm>
using namespace std;
 
int n;
char c;
bool visit[1001], ans[1001];
 
struct node
{
	int v1, v2;
}nod[1001];
 
bool cmp(node a, node b)
{
	if (a.v1 == b.v1)return a.v2 < b.v2;
	return a.v1 > b.v1;
}
 
int getmax()
{
	int max = 0, key = 0;
	for (int i = 1; i <= n; i++)
		if (!visit[i] && max <= nod[i].v2)max = nod[i].v2, key = i;
	return key;
}
 
bool ok(int k)
{
	int s = 0;
	for (int i = 1; i <= n; i++)
	{
		if (i == k || ans[i])s++;
		if (s > (i + (c == 'H'))/2)return false;
	}
	return true;
}
 
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> c;
		for (int i = 1; i <= n; i++)
		{
			cin >> nod[i].v1 >> nod[i].v2;
			visit[i] = false, ans[i] = false;
		}
		sort(nod + 1, nod + n + 1, cmp);
		int num = (n + (c == 'H')) / 2;
		visit[1] = (c == 'L');
		while (num)
		{
			int k = getmax();
			if (ok(k))ans[k] = true, visit[k] = true, num--;
			else for (int i = 1; i <= k; i++)visit[k] = true;
		}
		int s1 = 0, s2 = 0;
		for (int i = 1; i <= n; i++)
			if (ans[i])s2 += nod[i].v2; else s1 += nod[i].v1;
		cout << s1 << " " << s2 << endl;
	}
	return 0;
}

CSU 1043: Kegmore

Description

Recently, a popular online game League of Legends, similar to dota's 5V5 battle game, one of the long-range attack heroes, the abyss giant mouth Kegmore, is very powerful, standing and spraying during team battles may save the world...

We now calculate a simple data. Assuming that Kegmore encounters a sneak attack by five people on the opposite side, Kegmore can attack the enemy several times before he hangs up. The friendly forces are about to outflank, so Kegmore just wants to make as much as possible High damage does not pursue which one to kill. So, what is the highest output it can produce?

Input

The first line of each group of data is a positive integer n (0 <n <20), which represents the number of attacks that Kegmore can perform before hanging up, and the next five lines represent the damage that can be done to five enemies. There are two numbers in each line. The first number is the enemy's health, and the second number is the damage that an attack can produce. If the enemy's health value is less than 0 after one damage, the damage value this time is still calculated based on the damage value that can be made, and the dead enemy cannot be attacked next time. The enemy's health value does not exceed 5500, and the damage that an attack can produce does not exceed 550.

Output

Each set of data outputs one line, the highest damage Kegmore can hit.

Sample Input

10
1000 300
5000 200
4000 500
3000 100
2000 550

Sample Output

5200

Code:

#include<iostream>
#include<algorithm>
using namespace std;
 
struct node
{
	int sm, sh;
};
 
bool cmp(node a, node b)
{
	return a.sh > b.sh;
}
 
int main()
{
	int n;
	node nod[5];
	while (cin>>n)
	{
		for (int i = 0; i < 5; i++)cin >> nod[i].sm >> nod[i].sh;
		sort(nod, nod + 5, cmp);
		int ans = 0, t;
		for (int i = 0; n; i++)
		{
			t = (nod[i].sm - 1) / nod[i].sh + 1;
			if (t > n)t = n;
			n -= t, ans += t * nod[i].sh;
		}
		cout << ans << endl;
	}
	return 0;
}

 

Three, the choice of deque

OpenJ_Bailian - 3377 Best Cow Line, Gold

topic:

FJ is about to take his N (1 <= N <= 30,000) cows to the annual "Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges. 

The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (e.g., If FJ takes Bessie, Sylvia, and Dora in that order, he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order (i.e., alphabetical order) according to the string of the initials of the cows' names. 

FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them. 

FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order. 

Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.Input* Line 1: A single integer: N 

* Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original lineOutputThe least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the newline.Sample Input

6
A
C
D
B
C
B

Sample Output

ABCBCD

 

The meaning of the question is very simple. Given a sequence of numbers, take out one character at both ends each time to find the smallest lexicographical string that can be obtained.

Just be greedy.

Code:

#include<iostream>
using namespace std;
 
int main()
{
	int n;
	cin >> n;
	char s[30001];
	for (int i = 1; i <= n; i++)cin >> s[i];
	int left = 1, right = n, l, r;
	n = 0;
	while (left <= right)
	{
		l = left, r = right;
		while (s[l] == s[r] && l < r - 1)l++, r--;
		if (s[l] < s[r])cout << s[left++];
		else cout << s[right--];
		n++;
		if (n % 80 == 0)cout << endl;
	}
	return 0;
}

 

Guess you like

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