Danganronpa HDU - 5835

Chisa Yukizome works as a teacher in the school. She prepares many gifts, which consist of nn kinds with a[i]a[i] 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 T(T≤10)T(T≤10) indicating the number of test cases.

Each case contains one integer nn. The next line contains nn (1≤n≤10)(1≤n≤10) numbers: a1,a2,...,ana1,a2,...,an, (1≤ai≤100000)(1≤ai≤100000).

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

思路:两种普通礼物不相邻,神秘礼物每人都有。所以当最大的不大于sum - max时,人数sum/2;当最大的取不完即大于sum-max时。人数2*(sum-max)或sum的最小的那个

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<vector>
using namespace std;
int t, n,sum;
const int maxn = 15;
int a[maxn];
int c;
int main()
{
	cin >> t;
	c = 0;
	while (t--)
	{
		c++;		
		cin >> n;
		sum = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
			sum += a[i];
		}
		printf("Case #%d: ", c);
		sort(a, a + n);
		int ans = sum / 2;//神秘礼物不够时。
		sum -= a[n - 1];
		if (sum < a[n - 1])
		{
			sum *= 2;
		}
		else {
			sum += a[n - 1];
		}
		printf("%d\n", min(sum, ans));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/fighting_yifeng/article/details/81670464
hdu