贪心训练题组


前言

今日acm贪心训练题解,菜鸟进阶,代码肯定有不足之处,记录一下题解,有不足之处请斧正


一、Pasha and Tea

Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha’s friends. The i-th cup can hold at most ai milliliters of water.

It turned out that among Pasha’s friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows:

Pasha can boil the teapot exactly once by pouring there at most w milliliters of water;
Pasha pours the same amount of water to each girl;
Pasha pours the same amount of water to each boy;
if each girl gets x milliliters of water, then each boy gets 2x milliliters of water.
In the other words, each boy should get two times more water than each girl does.

Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha’s friends.

Input
The first line of the input contains two integers, n and w (1 ≤ n ≤ 105, 1 ≤ w ≤ 109) — the number of Pasha’s friends that are boys (equal to the number of Pasha’s friends that are girls) and the capacity of Pasha’s teapot in milliliters.

The second line of the input contains the sequence of integers ai (1 ≤ ai ≤ 109, 1 ≤ i ≤ 2n) — the capacities of Pasha’s tea cups in milliliters.

Output
Print a single real number — the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn’t exceed 10 - 6.

Examples
inputCopy
2 4
1 1 1 1
outputCopy
3
inputCopy
3 18
4 4 4 2 2 2
outputCopy
18
inputCopy
1 5
2 3
outputCopy
4.5

#include <iostream>
#include <algorithm>
using namespace std;
double a[200000];
int main()
{
	int n, w;
	scanf("%d%d", &n, &w);
	for (int i = 0; i < 2 * n; i++) {
		scanf("%lf", &a[i]);
	}
	sort(a, a + 2 * n);
	double num = (double)min(a[0] * 2, a[n]) /2.0;
	
	num =num*(3 * n);
	if (num > (double)w)	printf("%d", w);
	else	printf("%.6f", num);

	return 0;
}

二、Tian Ji – The Horse Racing(田忌赛马)

Here is a famous story in Chinese history.
That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others.

Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser.

Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian’s. As a result, each time the king takes six hundred silver dollars from Tian.

Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match.

It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king’s regular, and his super beat the king’s plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?

Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian’s horses on one side, and the king’s horses on the other. Whenever one of Tian’s horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching…

However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses – a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.

In this problem, you are asked to write a program to solve this special case of matching problem.
Input
The input consists of up to 50 test cases. Each case starts with a positive integer n ( n<=1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses. The input ends with a line that has a single `0’ after the last test case.
Output
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
Sample Input
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0
Sample Output
200
0
0

#include <iostream>
#include <algorithm>
using namespace std;
int a[1000], b[1000];
int n, ts, tk, qk, qs, win, lose;
//ts田忌慢马 tk田忌快马 qk齐王快马 qs齐王慢马
bool cmp(int a, int b) {
	return a > b;
}

int main()
{
	
	while (scanf("%d", &n) == 1 && n != 0) {
		win = lose = 0;
		qs = ts = n - 1; tk = qk = 0;
		for (int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		for (int i = 0; i < n; i++)
			scanf("%d", &b[i]);

		sort(a, a + n, cmp);
		sort(b, b + n, cmp);

		for (int i = 0; i < n; i++) {
			if (a[tk] > b[qk]) {
				win++;
				tk++;
				qk++;
			}
			else if (a[ts] > b[qs]) {
				win++;
				ts--;
				qs--;
			}
			else {
				if (a[ts] < b[qk]) {
					lose++;
					ts--;
					qk++;
				}
			}
		}
		int ans = (win - lose) * 200;
		printf("%d\n", ans);
	}
	return 0;
}

三、 Ilya and Diplomas

Soon a school Olympiad in Informatics will be held in Berland, n schoolchildren will participate there.

At a meeting of the jury of the Olympiad it was decided that each of the n participants, depending on the results, will get a diploma of the first, second or third degree. Thus, each student will receive exactly one diploma.

They also decided that there must be given at least min1 and at most max1 diplomas of the first degree, at least min2 and at most max2 diplomas of the second degree, and at least min3 and at most max3 diplomas of the third degree.

After some discussion it was decided to choose from all the options of distributing diplomas satisfying these limitations the one that maximizes the number of participants who receive diplomas of the first degree. Of all these options they select the one which maximizes the number of the participants who receive diplomas of the second degree. If there are multiple of these options, they select the option that maximizes the number of diplomas of the third degree.

Choosing the best option of distributing certificates was entrusted to Ilya, one of the best programmers of Berland. However, he found more important things to do, so it is your task now to choose the best option of distributing of diplomas, based on the described limitations.

It is guaranteed that the described limitations are such that there is a way to choose such an option of distributing diplomas that all n participants of the Olympiad will receive a diploma of some degree.

Input
The first line of the input contains a single integer n (3 ≤ n ≤ 3·106) — the number of schoolchildren who will participate in the Olympiad.

The next line of the input contains two integers min1 and max1 (1 ≤ min1 ≤ max1 ≤ 106) — the minimum and maximum limits on the number of diplomas of the first degree that can be distributed.

The third line of the input contains two integers min2 and max2 (1 ≤ min2 ≤ max2 ≤ 106) — the minimum and maximum limits on the number of diplomas of the second degree that can be distributed.

The next line of the input contains two integers min3 and max3 (1 ≤ min3 ≤ max3 ≤ 106) — the minimum and maximum limits on the number of diplomas of the third degree that can be distributed.

It is guaranteed that min1 + min2 + min3 ≤ n ≤ max1 + max2 + max3.

Output
In the first line of the output print three numbers, showing how many diplomas of the first, second and third degree will be given to students in the optimal variant of distributing diplomas.

The optimal variant of distributing diplomas is the one that maximizes the number of students who receive diplomas of the first degree. Of all the suitable options, the best one is the one which maximizes the number of participants who receive diplomas of the second degree. If there are several of these options, the best one is the one that maximizes the number of diplomas of the third degree.

Examples
Input
6
1 5
2 6
3 7
Output
1 2 3
Input
10
1 2
1 3
1 5
Output
2 3 5
Input
6
1 3
2 2
2 2
Output
2 2 2

#include <iostream>
using namespace std;
int main()
{
	int amax, amin, bmax, bmin, cmax, cmin , n;
	scanf("%d %d %d %d %d %d %d", &n, &amin, &amax, &bmin, &bmax, &cmin, &cmax);
	int now = n - amin - bmin - cmin;
	int res1 = amin; int res2 = bmin; int res3 = cmin;
	for (int i = res1; i < amax; i++) {
		if (now == 0)
			break;
		else {
			res1++; now--;
		}
	}
	for (int i = res2; i < bmax; i++) {
		if (now == 0)
			break;
		else {
			res2++; now--;
		}
	}
	for (int i = res3; i < cmax; i++) {
		if (now == 0)
			break;
		else {
			res3++; now--;
		}
	}
	printf("%d %d %d\n", res1, res2, res3);
	return 0;
}

四、 Delete

WLD likes playing with numbers. One day he is playing with N integers. He wants to delete K integers from them. He likes diversity, so he wants to keep the kinds of different integers as many as possible after the deletion. But he is busy pushing, can you help him?
Input
There are Multiple Cases. (At MOST 100)

For each case:

The first line contains one integer N(0<N≤100).

The second line contains N integers a1,a2,…,aN(1≤ai≤N), denoting the integers WLD plays with.

The third line contains one integer K(0≤K<N).
Output
For each case:

Print one integer. It denotes the maximum of different numbers remain after the deletion.
Sample Input
4
1 3 1 2
1
Sample Output
3

Hint
if WLD deletes a 3, the numbers remain is [1,1,2],he’ll get 2 different numbers.
if WLD deletes a 2, the numbers remain is [1,1,3],he’ll get 2 different numbers.
if WLD deletes a 1, the numbers remain is [1,2,3],he’ll get 3 different numbers.

#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int a[1000];
int main()
{
	int n; set<int> s;
	while (scanf_s("%d", &n) != EOF) {
		for (int i = 0; i < n; i++) {
			int a; scanf_s("%d", &a);
			s.insert(a);
		}
		int k; scanf_s("%d", &k);
		int cha = n - s.size();
		if (cha >= k)
			printf("%d\n", s.size());
		else
			printf("%d\n", n - k);
		s.clear();
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值