hdu 5835 || ccpc 2016 网络赛 1004 Danganronpa (模拟)

hdu 5835 题目链接 题意:n种礼物,每种a[i]个。现在有无穷个小朋友排成一排,分给每个人一个“普通”的礼物,一个“昂贵”的礼物(哪个普通哪个昂贵是自己定的,或者说,任意的) 要求是相邻的小朋友的普通的礼物不能是同一种。现在问最多能给多少小朋友分礼物。。。

思路:容易知道,因为昂贵的礼物是没有限制的。。所以没什么用。。考虑礼物总数sum..那么最多只可能分给sum/2个小朋友。。。然后再两个指针模拟一下。。记得特判n=1的情况。1A

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年08月14日 星期日 12时46分56秒
  4File Name :code/ccpc2016/1004.cpp
  5************************************************ */
  6#include <cstdio>
  7#include <cstring>
  8#include <iostream>
  9#include <algorithm>
 10#include <vector>
 11#include <queue>
 12#include <stack>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <deque>
 19#include <ctime>
 20#define fst first
 21#define sec second
 22#define lson l,m,rt<<1
 23#define rson m+1,r,rt<<1|1
 24#define ms(a,x) memset(a,x,sizeof(a))
 25typedef long long LL;
 26#define pi pair < int ,int >
 27#define MP make_pair
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=11;
 34int n;
 35int a[N];
 36int total;
 37bool bian[N];
 38bool cmp(int a,int b)
 39{
 40    return a>b;
 41}
 42int main()
 43{
 44	#ifndef  ONLINE_JUDGE 
 45	freopen("code/in.txt","r",stdin);
 46  #endif
 47	int T;
 48	cin>>T;
 49	int cas = 0 ;
 50	while (T--)
 51	{
 52	    printf("Case #%d: ",++cas);
 53	    scanf("%d",&n);
 54	    ms(bian,false);
 55	    total = 0 ;
 56	    for ( int i = 1 ; i <= n ; i++)
 57	    {
 58		scanf("%d",&a[i]);
 59		total +=a[i];
 60	    }
 61	    if (n==1)
 62	    {
 63		if (a[1]>1)
 64		puts("1");
 65		else puts("0");
 66		continue;
 67	    }
 68	    sort(a+1,a+n+1,cmp);
 69	    int R = total/2;
 70	    int cur = 0 ;
 71	    int i = 1;
 72	    int j = 2;
 73	    while (cur<R&&i<=n&&j<=n)
 74	    {
 75//		int x = a[i];
 76//		int y = a[j];
 77		if (a[i]==a[j])
 78		{
 79		    cur +=a[i]+a[j];
 80		    a[i] = 0;
 81		    a[j] = 0;
 82		    i = j+1;
 83		    j = i+1;
 84		}
 85		else  if (!bian[i])
 86		{
 87		    cur +=a[j]+a[j]+1;
 88		    a[i]-=(a[j]+1);
 89		    a[j] = 0;
 90		    j++;
 91		    if (a[i]<a[j]) swap(a[i],a[j]);
 92		    else bian[i] = true;
 93		}
 94		else
 95		{
 96		    cur +=a[j]+a[j];
 97		    a[i]-=a[j];
 98		    a[j] = 0;
 99		    j++;
100		    if (a[i]<a[j]) swap(a[i],a[j]);
101		}
102//		cout<<"cur:"<<cur<<endl;
103	    }
104	    cur = min(cur,R);
105	    printf("%d\n",cur);
106	}
107  #ifndef ONLINE_JUDGE  
108  fclose(stdin);
109  #endif
110    return 0;
111}