原题:
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?
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?
Each case contains one integer n . The next line contains n (1≤n≤10) numbers: a1,a2,...,an , (1≤ai≤100000) .
1 2 3 2
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;
}