HDU 5835 Danganronpa 【规律+贪心】

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(T10) indicating the number of test cases.

Each case contains one integer n. The next line contains n (1n10) numbers: a1,a2,...,an(1ai100000).

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种礼物,每个有ai个,现在开始给每个人发礼物,每人一个普通礼物和神秘礼物,

相邻两人的普通礼物必须不同,每个礼物都可以作为神秘礼物/普通礼物,问最多可以发给多少人。

大致思路:

先考虑看起来较为一般的情况:

随便列几组数据,发现结果都是sum/2

仔细想想也就知道最大的人数不会超过sum/2

然后考虑比较不一般的情况:

比如:1 1000这样

那么就可以先把个数最多的平铺在一条线上,让剩下的取插空,这样就能取最多的人数了。

然后把这个人数和sum/2取一个min,就是答案

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     ios::sync_with_stdio(false);
 6     int t,cnt=1,sum,maxx;
 7     int n,a[11];
 8     cin>>t;
 9     for(;cnt<=t;++cnt){
10         sum=0;
11         maxx=0;
12         cin>>n;
13         for(int i=0;i<n;++i){
14             cin>>a[i];
15             sum+=a[i];
16             maxx=max(maxx,a[i]);
17         }
18         cout<<"Case #"<<cnt<<": "<<sum/2<<endl;
19     }
20     return 0;
21 }
原文地址:https://www.cnblogs.com/SCaryon/p/7390741.html