Problem 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:
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
题意:有n种礼物,给出每种礼物的数量,将这些礼物放到排成一排的桌子里,每个礼物都可以看作是神秘礼物或者普通礼物
要求:
1、每个桌子里必有一件神秘礼物和一件普通礼物,
2、相邻两个桌子里的普通礼物不能一样,
3、神秘礼物没有限制,
4、放置礼物的桌子要排放在一起,也就是说中间不能有空桌子,
求至少能摆放多少张桌子。。
思路:
因为相邻桌子的普通礼物不一样,而神秘礼物没有限制,则只需考虑普通礼物的放置,,,
接下来就是模拟了,,
先从数量最多的礼物开始选,选完后将数量减一,并将标志位设为0(由于不能相邻,即下一轮不能选一样的),下一轮,再将标志位为0的设为1(即可选),依次重复多轮,
结束条件为:1、若选完一轮后只有一种礼物的数量不为0,
2、摆放桌子数最多为所有礼物的和/2(因为每张桌子只由两个礼物),
#include<stdio.h>
int main()
{
int t;
int n;
int i,k;
int a[15],flag[15];
scanf("%d",&t);
for(k=1;k<=t;k++)
{
int sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
flag[i]=1;
sum+=a[i];
}
int temp=sum/2; //求所有礼物数量和/2
int count=0;
int s;
while(count<temp)
{
int max=-10000000;
s=0;
int index;
for(i=0;i<n;i++)
{
if(flag[i])
{
if(max<a[i])
{ //每选一轮找出数量最多的那一种礼物
max=a[i];
index=i;
}
}
else
{
flag[i]=1;
}
if(a[i])
s++;
}
a[index]--;
flag[index]=0; //将已选礼物标志位变为0
count++;
if(s==1) //若只剩下一种礼物,则不能放置
break;
}
printf("Case #%d: %d\n",k,count);
}
return 0;
}
以下AC代码: