hdu 5835 题目链接
题意:n种礼物,每种a[i]个。现在有无穷个小朋友排成一排,分给每个人一个“普通”的礼物,一个“昂贵”的礼物(哪个普通哪个昂贵是自己定的,或者说,任意的) 要求是相邻的小朋友的普通的礼物不能是同一种。现在问最多能给多少小朋友分礼物。。。
思路:容易知道,因为昂贵的礼物是没有限制的。。所以没什么用。。考虑礼物总数sum..那么最多只可能分给sum/2个小朋友。。。然后再两个指针模拟一下。。记得特判n=1的情况。1A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
/* *********************************************** Author :111qqz Created Time :2016年08月14日 星期日 12时46分56秒 File Name :code/ccpc2016/1004.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <deque> #include <ctime> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; const int N=11; int n; int a[N]; int total; bool bian[N]; bool cmp(int a,int b) { return a>b; } int main() { #ifndef ONLINE_JUDGE freopen("code/in.txt","r",stdin); #endif int T; cin>>T; int cas = 0 ; while (T--) { printf("Case #%d: ",++cas); scanf("%d",&n); ms(bian,false); total = 0 ; for ( int i = 1 ; i <= n ; i++) { scanf("%d",&a[i]); total +=a[i]; } if (n==1) { if (a[1]>1) puts("1"); else puts("0"); continue; } sort(a+1,a+n+1,cmp); int R = total/2; int cur = 0 ; int i = 1; int j = 2; while (cur<R&&i<=n&&j<=n) { // int x = a[i]; // int y = a[j]; if (a[i]==a[j]) { cur +=a[i]+a[j]; a[i] = 0; a[j] = 0; i = j+1; j = i+1; } else if (!bian[i]) { cur +=a[j]+a[j]+1; a[i]-=(a[j]+1); a[j] = 0; j++; if (a[i]<a[j]) swap(a[i],a[j]); else bian[i] = true; } else { cur +=a[j]+a[j]; a[i]-=a[j]; a[j] = 0; j++; if (a[i]<a[j]) swap(a[i],a[j]); } // cout<<"cur:"<<cur<<endl; } cur = min(cur,R); printf("%d\n",cur); } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!