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 2Sample Output
Case #1: 2Source
2016中国大学生程序设计竞赛 - 网络选拔赛##题意: 有n种礼物,每种的个数为Ai. 现在要把礼物分发给小朋友,求最多能发给多少人. (人数无限) 每个人要发两件礼物,其中一件的种类不限. 而另一件必须与相邻的人不同种类.
##题解: 由于神秘礼物的种类不限,所以没有必要考虑它,维护一下个数即可. 一开始以为每个位置只要不同即可,随便放哪个都行. 不过显然是错的(100,2,2). (很遗憾数据巨水,sum/2也能过) 贪心的想法就是把数量多的礼物先放下,在间隔的位置放下数量少的礼物. 即第一个人放一个最多的,第二个人放一个最少的...依次类推. 这样可以保证放的个数最多. 因为把最难用掉的(数量最多的)尽量用掉了.
上述思路是模拟,其实不需要模拟. 首先每种情况的最大答案就是SUM/2; 考虑上面说的那种错误的情况(100,2,2),之所以这时候达不到SUM/2,是因为某种礼物的数量超过了其余种类的数量和. 当某种的数量超过其它的和时:我们可以这样摆放(先不考虑神秘礼物) 最多的种类 与 其他种类 交替摆放成一行. ABAB..A 把剩下的礼物(一定是数量最多的那个种类)当作神秘礼物依次填充: 如果填充不满,那么就还需要从刚才的单行末尾抽取礼物来作神秘礼物,这样一来答案还是SUM/2; 如果神秘礼物填充满了,而且还有剩下,那么结果就是 (sum-max)*2 + 1.
##代码: ``` cpp #include