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 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 (贪心)的更多相关文章
- HDU 5835 Danganronpa 贪心
Danganronpa 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...
- HDU 5835 Danganronpa(弹丸论破)
Danganronpa(弹丸论破) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5835 Danganronpa
Danganronpa Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 5835 Danganronpa (水题)
题意:给定 n 个礼物有数量,一种是特殊的,一种是不特殊的,要分给一些人,每人一个特殊的一个不特殊,但是不特殊的不能相邻的,问最多能分给多少人. 析:是一个比较简单的题目,我们只要求差值就好,先算第一 ...
- [ An Ac a Day ^_^ ] hdu 5835 Danganronpa 令人发指
这道题告诉我两个道理: 1.记得写case 要不挂死你 2.数据很水的时候 只有样例的这一种情况…… 原来数据可以这么水…… #include<stdio.h> #include<i ...
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
- Hdu 4864(Task 贪心)(Java实现)
Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...
- D - 淡黄的长裙 HDU - 4221(贪心)
D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...
- 【HDU 5835】Danganronpa(分配礼物)
10种礼物,每种有ai个,每个小朋友分两个礼物,其中普通礼物要求相邻两人的不能一样,求最多分给几个小朋友. sum/2是最多的情况.什么时候发不了那么多,就是当max很大,无论怎么发,都发不完max. ...
随机推荐
- struct hw_module_t HAL_MODULE_INFO_SYM
先开个头,准备这与一篇struct hw_module_t HAL_MODULE_INFO_SYM 相关的文章. Hal层的库文件是怎么被上层调用的?上层调用时的入口(相当于main)又是什么呢?它就 ...
- [原]用WebBrowser组件模拟人工运行搜索引擎自动点击搜索结果的实验
本代码只是业余时间无聊写着试试,用WebBrowser组件模拟人工运行搜索引擎自动点击搜索结果的实验 这是网络中盛传的提高搜索引擎点击率的一种方式,当然属于作弊,不推荐各位使用.另外这种方式的性能不佳 ...
- struts2的@Result annotation 如何添加params
参考: http://struts.apache.org/2.0.11/docs/result-annotation.html http://jdkcn.com/entry/add-params-to ...
- POJ 2455 - Secret Milking Machine
原题地址:http://poj.org/problem?id=2455 题目大意:给出一个N个点的无向图,中间有P条边,要求找出从1到n的T条通路,满足它们之间没有公共边,并使得这些通路中经过的最长的 ...
- bzoj2794
这题我得到一个经验,bool型的dp一定要想办法把bool去掉来表示更多的东西(1933也是这个道理) 暴力大家都会,这里有两个限制条件 一个限制条件我们可以排序不断加入,另一个呢 我们可以用f[i] ...
- [Sciter系列] MFC下的Sciter–3.Sciter脚本与底层交互
[Sciter系列] MFC下的Sciter–3.Sciter脚本与底层交互,脚本调用底层自定义的方法函数. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的SciterF ...
- Java [Leetcode 102]Binary Tree Level Order Traversal
题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- 在Windows下编译ffmpeg完全手册
本文的内容几乎全部来自于FFmpeg on Windows,但是由于国内的网络封锁,很难访问这个域名下的内容,因此我一方面按照我自己的理解和实践做了翻译,另一方面也是为了能提供一个方便的参考方法. 注 ...
- MyBatis 如何接收参数
MyBatis的mapper接口不需要自己实现,框架会自动帮我们实现,到时候直接调用就可以了.定义的mapper接口中的方法可以有多个参数吗?答案是肯定.在Ibatis时代是自己通过代码实现如何调用x ...
- CPC23-4 K.喵喵的神·数
题意:给出整数T,P,求c(T,P) mod P. 解法:用卢卡斯定理. 卢卡斯定理:解决c(n,m) mod p问题.Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p) ...