HDU 5835 Danganronpa (贪心)

hdu 5835

题目:

Problem Description

Chisa Yukizome works as a teacher in the school. She prepares many gifts, which consist of n kinds with 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) indicating the number of test cases.

Each case contains one integer n. The next line contains n (1≤n≤10) numbers: a1,a2,...,an, (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

题意:这题目太难读了, 真正的题意是给N种礼物, 然后是每种礼物的个数, 每个礼物都可以做为普通礼物和神秘礼物。学生站一排, 然后分发礼物, 相邻学生的普通礼物不能是同种,而神秘礼物没有限制, 求最多可以有多少学生可以获得普通和神秘礼物。

题解:读明白题应该就比较好想了, 首先要知道答案的最大值只可能是sum / 2, 然后我们先把这些礼物中最多的礼物直接一字排开,再把剩下的礼物从刚才一字排开的起点依次填充这样的话会有三种情况 :

maxx表示最多的那种礼物, sum表示礼物总数。

(1). maxx  <= sum - maxx || maxx - (sum - maxx) < maxx + 2  此时是可以取最大值 sum / 2的。

(2). maxx - (sum - maxx) > maxx + 2 此时不能取到sum / 2, 结果是 2 * (sum - maxx) + 1。

最后只用一个式子就可以计算结果:

ans = min(sum / 2, 2 * (sum - maxx) + 1)。

题解里说了思考方式, 分析过程不想多说~

AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int i, j, n, ans, T, t = 1, k;
    int maxx, sum;
    int s[12];
    scanf("%d", &T);
    while(T--){
        sum = 0, maxx = 0;
        scanf("%d", &n);
        for(i = 0;i < n;i++){
            scanf("%d", &s[i]);
            sum += s[i];
            maxx = max(s[i], maxx);
        }
        ans = min(sum / 2, 2 * (sum - maxx) + 1);
        printf("Case #%d: %d\n",t++, ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LxcXingC/article/details/81667061