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

Source


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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define double long long
#define eps 1e-8
#define maxn 101000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int main(int argc, char const *argv[])

{

//IN;

int t, ca=1; cin >> t;
while(t--)
{
int n; scanf("%d", &n);
int sum = 0, maxx = 0;
for(int i=1; i<=n; i++) {
int x; scanf("%d", &x);
sum += x;
maxx = max(maxx, x);
} int ans = min(sum/2, (sum-maxx)*2+1); printf("Case #%d: %d\n", ca++, ans);
} return 0;

}


####大小交替模拟:
``` cpp
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; int a[20];
int main(int argc, char const *argv[])
{
//freopen("in.txt", "r", stdin);
int T;
int n;
scanf("%d", &T);
int cas = 0;
while (T--){
scanf("%d", &n);
int sum = 0;
for (int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
sum += a[i];
}
sort(a + 1, a + n + 1);
int p = 1, q = n;
int res = 0;
while (p != q){
int x = min(a[p], a[q]);
res += x * 2;
a[p] -= x;
a[q] -= x;
if (a[p] == 0) ++p;
if (a[q] == 0) --q;
}
int ans = min(sum / 2, res);
printf("Case #%d: %d\n", ++cas, ans);
} }

HDU 5835 Danganronpa (贪心)的更多相关文章

  1. HDU 5835 Danganronpa 贪心

    Danganronpa 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...

  2. HDU 5835 Danganronpa(弹丸论破)

     Danganronpa(弹丸论破) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. HDU 5835 Danganronpa

    Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. HDU 5835 Danganronpa (水题)

    题意:给定 n 个礼物有数量,一种是特殊的,一种是不特殊的,要分给一些人,每人一个特殊的一个不特殊,但是不特殊的不能相邻的,问最多能分给多少人. 析:是一个比较简单的题目,我们只要求差值就好,先算第一 ...

  5. [ An Ac a Day ^_^ ] hdu 5835 Danganronpa 令人发指

    这道题告诉我两个道理: 1.记得写case 要不挂死你 2.数据很水的时候 只有样例的这一种情况…… 原来数据可以这么水…… #include<stdio.h> #include<i ...

  6. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

  7. Hdu 4864(Task 贪心)(Java实现)

    Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...

  8. D - 淡黄的长裙 HDU - 4221(贪心)

    D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...

  9. 【HDU 5835】Danganronpa(分配礼物)

    10种礼物,每种有ai个,每个小朋友分两个礼物,其中普通礼物要求相邻两人的不能一样,求最多分给几个小朋友. sum/2是最多的情况.什么时候发不了那么多,就是当max很大,无论怎么发,都发不完max. ...

随机推荐

  1. MySql开启跟踪

    使用 show variables like '%log%'; 查看general_log.log_output.general_log_file 更新为 set global general_log ...

  2. 客户视角:Oracle ETL工具ODI

    客户视角:Oracle ETL工具ODI 数据集成已成为企业在追求市场份额中的关键技术组件,与依靠手工编码的方式不同,越来越多的企业选择完整的数据集成解决方案来支持其IT战略,从大数据分析到云平台的集 ...

  3. 百度HTTPS加密搜索有什么用?

    前段时间,我曾提到百度支持移动端HTTPS SSL加密搜索,用以保护用户隐私.最近,百度开始支持PC端HTTPS SSL加密搜索,现在可以启用 https://www.baidu.com 搜索.我很少 ...

  4. 转:表单中Readonly和Disabled的区别(HTML中使用javascript解除禁止input输入框代)

    转:表单中Readonly和Disabled的区别 参考资料: disabled和readonly区别: 参考博文1地址:http://blog.csdn.net/symgdwyh/article/d ...

  5. ASCII码常用值

    大写字母 A~Z 65~90 小写字母a~z 97~122 数字0~9 48~ 57

  6. C#计算程序执行速度

    long t1 = DateTime.Now.Ticks; //执行程序,例如处理100个文件 long t2 = DateTime.Now.Ticks; Response.Write("执 ...

  7. javascript中的关键字和保留字

    javascript中关键字的问题,将名称替换了下,确实就没有问题了.现在将它的关键字和保留字贴出来,便于日后查看和避免在次出现类似的问题. 1 关键字breakcasecatchcontinuede ...

  8. [转] 搜索之双向BFS

    转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...

  9. JQuery Ajax 在asp.net中使用小结

    自从有了JQuery,Ajax的使用变的越来越方便了,但是使用中还是会或多或少的出现一些让人短时间内痛苦的问题.本文暂时总结一些在使用JQuery Ajax中应该注意的问题,如有不恰当或者不完善的地方 ...

  10. 简单分析什么是SQL注入漏洞

    现在很多人在入侵的过程中基本都是通过SQL注入来完成的,但是有多少人知道为什么会有这样的注入漏洞呢?有的会随口说着对于字符的过滤不严造成的.但是事实是这样吗?我们学这些,不仅要知其然,更要知其所以然! ...