HDU 5835 Danganronpa (贪心)

来源:互联网 发布:大数据前沿技术与应用 编辑:程序博客网 时间:2024/05/16 06:03

Danganronpa

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 376    Accepted Submission(s): 27

Problem Description
Chisa Yukizome works as a teacher in the school. She prepares many gifts, which consist ofn 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 integerT(T10) indicating the number of test cases.

Each case contains one integer n. The next line contains n(1n10) numbers: a1,a2,...,an,(1ai100000).
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
123 2
Sample Output
Case #1: 2
Author
UESTC
Source
2016中国大学生程序设计竞赛 - 网络选拔赛
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5842 5841 5840 5838 5837 

题意:给你有n种礼物,第i种有ai个。现在要把这些礼物分给一排同学,要求每个人发普通礼物和神秘礼物各一个,要求相邻两人的普通礼物不能为同一种,神秘礼物没有限制,普通礼物和神秘礼物都从这n个礼物中选取。问最多能发给多少人。

题解:贪心一下即可。
因为每个人都要拿到两份礼物,所以人数最多为sum/2。

我们先考虑数量最多的那种礼物,因为能分给多少人就取决于这种数量最多的礼物(可以当作神秘礼物)啊。我能先把除了数量最多的其他礼物间隔留空地放,然后让数量最多的礼物去插空。如果插空不满,我就拿后面的礼物当神秘礼物,继续插空。这样是sum/2。如果神秘礼物插空满了,还有剩下的,就是(sum-maxx)*2+1。(手动模拟一下吧)

AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")//#include<bits/stdc++.h>#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<map>#include<cmath>#include<queue>#include<set>#include<stack>#include <utility>using namespace std;typedef long long ll;typedef unsigned long long ull;#define mst(a) memset(a, 0, sizeof(a))#define M_P(x,y) make_pair(x,y)  #define rep(i,j,k) for (int i = j; i <= k; i++)  #define per(i,j,k) for (int i = j; i >= k; i--)  #define lson x << 1, l, mid  #define rson x << 1 | 1, mid + 1, r  const int lowbit(int x) { return x&-x; }  const double eps = 1e-8;  const int INF = 1e9+7; const ll inf =(1LL<<62) ;const int MOD = 1e9 + 7;  const ll mod = (1LL<<32);const int N = 100; const int M=100010; template <class T1, class T2>inline void getmax(T1 &a, T2 b) {if (b>a)a = b;}  template <class T1, class T2>inline void getmin(T1 &a, T2 b) {if (b<a)a = b;}int read(){int v = 0, f = 1;char c =getchar();while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();return v*f;}int a[N];int main(){#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);    #endif    int t,cas=1; t=read();    while(t--)    {        int n;n=read();int x;        int sum = 0, maxx = 0;        for(int i=1; i<=n; i++) {             scanf("%d", &x);            sum += x;            maxx = max(maxx, x);        }        int ans = min(sum/2, (sum-maxx)*2+1);        printf("Case #%d: %d\n", cas++, ans);    }return 0;}


2 0
原创粉丝点击