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. .net 生成拼音码与五笔码

    首先加入配置文件: <?xml version="1.0" encoding="utf-8" ?> <CodeConfig> <S ...

  2. word引用错误

    错误 4317 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”.请改用适用的接口. 类型“Microsoft.Office.Inte ...

  3. CSS+DIV问题!DIV的最小高度问题!

    DIV层的最小高度问题!就是一个DIV有个最小高度,但是如果DIV层中的内容很多,DIV的高度会根据内容而进行拉长!要求IE6.IE7还有firefox都要兼容!我试了很多网上的方法都不好用!请测试后 ...

  4. HDU 1711 (裸KMP) Number Sequence

    题意: 用第二个数列去匹配第一个数列,输出第一次匹配到的位置,如果没有则输出-1. 分析: 这明显是一道裸的KMP. 我是在这篇博客上学的KMP算法的,讲得比较透彻. http://blog.csdn ...

  5. Asp.Net连接Mysql报错Out of sync with server

    Asp.Net连接Mysql报错Out of sync with server 原因:程序引用的MySql.Data.dll版本高于服务器版本 解决:下载一个低版本的MySql.Data.dll,项目 ...

  6. System.web.optimization 在 Asp.Net WebForm 中应用得注意了

    我们也可以在Asp.Net WebForm项目中去使用Optimization,去处理我们的资源文件,从而起到优化网站性能的效果,前端知识得从小事做起.但是在使用过程中我却发现了下面的问题. 第一步: ...

  7. ti processor sdk linux am335x evm /bin/setup-minicom.sh hacking

    #!/bin/sh # # ti processor sdk linux am335x evm /bin/setup-minicom.sh hacking # 说明: # 本文主要对TI的sdk中的s ...

  8. ffmpeg/ffplay vc6 源码剖析

    ffmpeg/ffplay是当今多媒体领域的王者,很多很多的人想研究学习ffmpeg/ffplay,但苦于ffmpeg/ffplay庞大的代码量,令人望而生畏.为帮助更多的人研习ffmpeg/ffpl ...

  9. GitHub开源库排名一百的简单介绍,值得收藏!

    GitHub Android Libraries Top 100 简介 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitHub ...

  10. 走向DBA[MSSQL篇] - 从SQL语句的角度提高数据库的访问性能(转)

    最近公司来一个非常虎的DBA,10几年的经验,这里就称之为蔡老师吧,在征得我们蔡老同意的前提下 ,我们来分享一下蔡老给我们带来的宝贵财富,欢迎其他的DBA来拍砖.  目录 1.什么是执行计划?执行计划 ...