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. ...
随机推荐
- Storm集群的搭建
storm的环境和hadoop的环境没有任何关系 1.安装Zookeeper集群 2.解压storm 3.修改文件conf/storm.yaml 3.1.配置zookeeper服务器 storm.zo ...
- WIN7 64位系统搭建WINCE6.0系统遇到的问题
WIN7 64位系统搭建WINCE6.0系统遇到的问题 安装顺序如下: .先装Visual Studio2005: .安装Visual Studio2005 Service Pack 1: .安装Vi ...
- ViewPager介绍和使用说明
1 ViewPager实现的功能 和实际运行的效果图示意 ViewPager类提供了多界面切换的新效果.新效果有如下特征: [1] 当前显示一组界面中的其中一个界面. [2] 当用户通过左右滑动界 ...
- IIS与ASP.NET中的队列
一.IIS:应用程序池队列(Application pool queue,位于HTTP.SYS) 这是请求到达IIS后遇到的第一个队列,http.sys收到请求后会将请求放入对应的应用程序池队列,这样 ...
- LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)
题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序 ...
- liux之我用过的zip解压命令
用途说明 zip文件是一种常用的压缩文件格式,WinZip.WinRar等压缩软件都支持zip文件格式,就连java的jar包也是zip格式 的,Firefox插件xpi文件也是zip格式的.Linu ...
- 2014年acm亚洲区域赛·鞍山站
今天北京赛站的比赛也结束了···看了一天的直播之后意识到鞍山站的比赛都过去了一个多月了···这一个月比较萎靡···整天都在睡觉写报告画工图中度过··· 鞍山比哈尔滨还是暖和很多的···就是山上有奇怪的 ...
- 省市区(县)三级联动代码(js 数据源)
ylbtech-JavaScript-Utility:省市区(县)三级联动代码(js 数据源) 省市区(县)三级联动代码(js 数据源) 1.A,源代码(Source Code)返回顶部 1.A.1, ...
- mysql show processlist命令 详解
SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到所有线程.否则,您只能看到您自己的线程( ...
- mysql DDL语句
sql语言分为三个级别. 1.ddl 语句 ,数据定义语句,定义了数据库.表.索引等对象的定义.常用语句包含:create.drop.alter. 2.dml 语句 ,数据操纵语句,用于添加.删除.更 ...