HDU 5835 Danganronpa

原题:

Chisa Yukizome works as a teacher in the school. She prepares many gifts, which consist of  n n kinds with  a[i] 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(T10) T(T≤10) indicating the number of test cases. 

Each case contains one integer  n n. The next line contains  n n  (1n10) (1≤n≤10) numbers:  a1,a2,...,an a1,a2,...,an (1ai100000) (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种礼物,每种有a[i]个,一些为神秘礼物,一些为普通礼物,将这些礼物排列,每一组必须要有一个神秘礼物和一个普通礼物,相邻的组普通礼物必须互不相同,最多可以排多少组。


思路:

      首先计算所有礼物每组一个相邻互不相同可以排多少组,可以得知,当数量最多的一种比其他所有种礼物数量加起来还要多1个以上时,则可以排(sum-a[max])*2+1组,其他情况则全部可以排成相邻互补相同,即sum组。

     当为第一种情况,即不能全部排完时,又分两种情况。1、当排列礼物数小于等于无法排列的礼物时,则可以把剩下的礼物当作神秘礼物分发到排列好的每一组去,则答案为(sum-a[max])*2+1。2、当排列礼物数小于等于无法排列的礼物时,则需要一部分排列的礼物和所有的无法排列的礼物为神秘礼物,则答案为sum/2。

    当为第二种情况时,则把一半的排列的礼物当作神秘礼物分发到前一半里,则答案为sum/2。


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <deque>
#include <string>
#include <cmath>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <sstream>
#include <climits>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define pi acos(-1.0)
#define INF 2147483647
using namespace std;
typedef long long ll;
typedef pair <int,int > P;
const int maxn=100005;  
int a[maxn];
int n,i,j,t;
int main()
{
    scanf("%d",&t); 
    for(int k=1; k<=t; k++)
    {
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        int sum=0;
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        sort(a,a+n,greater<int >() );
        sum-=a[0];
        if(a[0]-sum>1)
        {
            int ans1=sum*2+1;
            int ans2=sum+a[0]-ans1;
            if(ans1<=ans2)
                printf("Case #%d: %d\n",k,ans1);
            else
                printf("Case #%d: %d\n",k,(sum+a[0])/2);

        }
        else
        {
            int ans=sum+a[0];
            printf("Case #%d: %d\n",k,ans/2);
        }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值