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. 字符设备 register_chrdev_region()、alloc_chrdev_region() 和 register_chrdev()

    1. 字符设备结构体 内核中所有已分配的字符设备编号都记录在一个名为 chrdevs 散列表里.该散列表中的每一个元素是一个 char_device_struct 结构,它的定义如下: static ...

  2. NDK(21)JNI的5大正确性缺陷及优化技巧(注意是正确性缺陷)

    转自 : http://www.ibm.com/developerworks/cn/java/j-jni/index.html JNI 编程缺陷可以分为两类: 性能:代码能执行所设计的功能,但运行缓慢 ...

  3. css3 去掉点击高光(移动端)

    在我们用手机浏览网页时,当点击某个链接或者点击事件时 会出现一层蓝色半透明的高光显示, 但在开发webapp时,由于网页是被native load去的,在app里面点击的时候如果出现高光则显得很不和谐 ...

  4. ACM刷题常用链接

    武汉科技大学  http://acm.wust.edu.cn/ 华中科技大学 http://acm.hust.edu.cn/vjudge/toIndex.action 杭州电子科技大学  http:/ ...

  5. ASP.NET 数据绑定常用代码及其性能分析

    用DataBinder.eval 绑定不必关心数据来源(Dataread或dataset).不必关心数据的类型eval会把这个数据对象转换为一个字符串.在底层绑定做了很多工作,使用了反射性能.正因为使 ...

  6. hdu 1257 最少拦截系统(简单贪心)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1257 虽然分类是dp感觉还是贪心 比较水 #include <iostream> #inclu ...

  7. 石子合并(四边形不等式优化dp) POJ1160

    该来的总是要来的———————— 经典问题,石子合并. 对于 f[i][j]= min{f[i][k]+f[k+1][j]+w[i][j]} From 黑书 凸四边形不等式:w[a][c]+w[b][ ...

  8. ha_innobase::rnd_next

    /*****************************************************************//** Reads the next row in a table ...

  9. 函数buf_read_page

    /********************************************************************//** High-level function which ...

  10. 基于XMPP的即时通信系统的建立(一)— XMPP基础概念

    相关背景 IM(Instant Messaging)正在被广泛使用,特别是公司与它们的客户互动连接方案以及互联网与Web2.0相关的应用.为了解决即时通信的标准问题,IETF(互联网工程任务组 The ...