杭电5835之Danganronpa

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:

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) 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
  
  
1 2 3 2
 

Sample Output

Case #1: 2

题意:

给你n种礼物,礼物个数分别为a1,a2....an

每种礼物可作为神秘礼物或普通礼物,若干个桌子排成一排,每个桌子上必须有一个神秘礼物和一个普通礼物,相邻的桌子普通礼物不能相同,神秘礼物无此限制

问最多由多少个桌子的礼物符合要求(题中为,有多少个同学可以获得合理的礼物)

思路:先排序,假设只有两种礼物a1和a2(a1<a2),考虑两种情况,a2>=3*a1+2,a2<3*a1+2

当a1,a2相差不大时,结果为(a1+a2)/2,相差较大时结果为a1+(a1+1),满足此条件时,a2>=a1+2*(a1+1)

例如,1,10

1,22222二二二二二

小写的表示不同礼物,分法为 2二 1二 2二 ,结果为2*1+1=3,(10>1+2*(1+1))

读者自己体会

AC代码如下:

#include "iostream"
#include "cstdio"
#include "algorithm"
using namespace std;


int a[100];


int main(int argc, char* argv[])
{
	int cas,n,i;
	cin>>cas;
	int ss=0;
	while(cas--)
	{	
		cin>>n;
		printf("Case #%d: ",++ss);
		int sum=0;
		for (i=0;i<n;i++)
		{
			cin>>a[i];
		}
		sort(a,a+n);
		for (i=0;i<n-1;i++)
		{
			sum+=a[i];
		}
		if (a[n-1]>=3*sum+2)
		{
			cout<<2*sum+1<<endl;
		}
		else
		{
			cout<<(sum+a[n-1])/2<<endl;
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值