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(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

题目大意:

题意:有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 }
时间: 2024-10-24 22:39:58

HDU 5835 Danganronpa 【规律+贪心】的相关文章

HDU 5835 Danganronpa(弹丸论破)

HDU 5835 Danganronpa(弹丸论破) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 题目描述 Chisa Yukizome works as a teacher in the school. She prepares many gifts, which consist of n kinds with a[i] quantities of

hdu 5835 Danganronpa 贪心+水题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5835 [题意]有n种礼物,每个有ai个,现在开始给每个人发礼物,每人一个普通礼物和神秘礼物,相邻两人的普通礼物必须不同,每个礼物都可以作为神秘礼物/普通礼物,问最多可以发给多少人. [解题思路] 答案肯定是小于等于sum/2,因为每个小朋友得有2礼物.然而数据太水,sum/2也过了. 我们先考虑平凡的礼物,因为平凡的礼物相邻桌子上不能相同,故先把数量最多的礼物相邻的交替用作平凡的礼物. 如果n=3,

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

这道题告诉我两个道理: 1.记得写case 要不挂死你 2.数据很水的时候 只有样例的这一种情况…… 原来数据可以这么水…… 1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<math.h> 5 #include<string.h> 6 #include<string> 7 #include<map> 8 #include&l

HDU 5835 Danganronpa (水题)

题意:给定 n 个礼物有数量,一种是特殊的,一种是不特殊的,要分给一些人,每人一个特殊的一个不特殊,但是不特殊的不能相邻的,问最多能分给多少人. 析:是一个比较简单的题目,我们只要求差值就好,先算第一个和第二个的差值,再算第三个和第一个和第二个的差值的差值,直到最后,最后再判一下,是不是有剩下的, 如果有,那么还可以分不分. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

hdu 4864 Task (贪心)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int t; int v; int yy; }; struct node a[100010],b[100010]; bool cmp(node a1,node a2) { if(a1.t==a2.t)//先按时间从大到小 return a1.v>a2.v;//再按水平从大

HDU 4279 Number 规律题

题意: 定义函数F(x) : 区间[1,x]上的y是满足:GCD(x,y)>1 && x%y>0的 y的个数. 问:对于任意区间[l,r] 上的F(l···r) 有几个函数值是奇数的. 打表找规律. 打的是[1,x]区间的结果 把所有结果不相同的值打出来(因为结果是递增的,所以只观察不相同的结果) 发现:ans = x/2-2 || x/2-1 再把所有结果不同 && x/2-1的值打出来 发现 sqrt(x) &1 == 1 得到:ans = x/2-

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

HDU 4903 (模拟+贪心)

Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a heat for years in China. The game goes with the 54 poker cards for 3 players, where the “Landlord” has 20 cards and the other two (the “Farmers”) have 1