HDU 5835 Danganronpa 贪心

Danganronpa

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5835

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

Hint

题意

有n类礼物,每个礼物有a[i]个,然后你要分发给尽量多的小朋友,使得每个小朋友都有一个神秘礼物和普通礼物,要求相邻的小朋友的普通礼物都不相同。

题解:

首先答案肯定是小于等于sum/2,因为每个小朋友得有2礼物。

然后我们考虑最大的礼物,我们先每个小朋友一个大礼物,然后再分其他礼物。

相邻的交替用大礼物当神秘礼物就好了。

至于sum/2,这个显然是错的啊,比如数据就一种礼物,这个礼物有5个。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
int a[maxn],cas;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,mx=0,sum=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            mx=max(a[i],mx);
            sum+=a[i];
        }
        int ans=min(sum/2,(sum-mx)*2+1);
        printf("Case #%d: %d\n",++cas,ans);
    }
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5771084.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞