Danganronpa HDU

来源:互联网 发布:西安云计算公司 编辑:程序博客网 时间:2024/05/29 14:34

Danganronpa

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 937    Accepted Submission(s): 636


Problem Description
Chisa Yukizome works as a teacher in the school. She prepares many gifts, which consist ofn 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
123 2
 

Sample Output
Case #1: 2
 

Author
UESTC
 

Source
2016中国大学生程序设计竞赛 - 网络选拔赛

短板效应吧
总数是sum,最大情况就是sum/2;
设最大数量为max      
1.若max <= sum-max(剩下的)   即 max < sum/2
此时将数量最大的礼物插孔放完【全做神秘礼物】之后可用其他礼物来补,此时绝对可补到sum/2
2.若max > sum-max    即max > sum/2  ,  此时短板是除了数量最多的礼物的其他礼物,将除了数量最多的其他礼物插孔放后放数量最多的礼物
(sum-max)*2+1。

解释半天好像也解释不清楚。。。 还是手动模拟比较好吧

#include<cstdio>#include<cstring>#include<cstdlib>#include<queue>#include<cmath>#include<algorithm>using namespace std;#define maxn  100005int a[maxn];int  main(){int T,i,sum,ans,maxs,n,cases=0;scanf("%d",&T);while(T--){maxs=0;sum=0;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&a[i]);sum+=a[i];maxs=max(maxs,a[i]);}int ans=min(sum/2,(sum-maxs)*2+1);printf("Case #%d: %d\n",++cases,ans);}}
2.
优先队列实现贪心  代码来源:点击打开链接
    #include <iostream>      #include <cstdio>      #include <cstring>      #include <algorithm>      #include <cmath>      #include <queue>      using namespace std;      priority_queue<int>que;      int main()      {          int T,x,n,t=1;          scanf("%d",&T);          while(T--)          {              while(!que.empty())                  que.pop();              scanf("%d",&n);              int sum=0,ans=0,k=0,l,r;              for(int i=0;i<n;i++)                  {                      scanf("%d",&x);                      sum+=x;                      que.push(x);                  }              while(!que.empty())              {                  if(!k)                  {                      l=que.top();                      que.pop();                      if(que.empty()&&l>1)                      ans++;                  }                  else                  {                      r=que.top();                      que.pop();                      ans+=2*r;                      que.push(l-r);                  }                  k^=1;              }               printf("Case #%d: %d\n",t++,min(ans,sum/2));          }          return 0;      }