您的位置:首页 > 其它

hdu 5835 Danganronpa(贪心,优先队列)

2016-08-14 23:32 344 查看


Danganronpa

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 228    Accepted Submission(s): 161


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(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

 

Author

UESTC

 

Source

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

题意:有n种礼物,每种礼物有a[i]个,现在有很多个桌子, 每个桌子放两个礼物,你可以任意选择一个的当普通的礼物,另一个是特殊的礼物。要求相邻两个桌子的普通的礼物不能相同,问最多能摆几张桌子

思路:比赛的时候看见这题一脸懵逼,不知道那群看到这题就觉得是sum/2的人怎么想的。sum/2应该是错的只是因为数据水了才过了。

朋友想到了一个区间DP的解法,比赛的时候虽然AC了,不过 赛后我看了别人思路才发现正解应该是贪心+优先队列。

对于这个问题我们知道 数量越多的礼物价值越低(最后剩太多的话没得放了),所以我们贪心得把当前前两大的礼物取出来,插空摆放,然后把最大-次大的值在放进优先队列里面。表示现在还有最大- 次大个当前价值最大的礼物,以此类推直到队列为空。

可想而知这种贪心策略是可行的。

代码:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: