您的位置:首页 > 其它

HDU4974 A simple water problem(贪心)

2015-07-30 22:15 330 查看

A simple water problem

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon’s favorite. After each competition he will give a score of either 0 or 1 for each competitor and add it to the total score of that competitor. The total score begins with zero. Here’s an example: four competitors with name James, Victoria, Penghu, and Digo. First goes a competition between Penghu and Digo, and Dragon enjoys the competition and draw both 1 score for them. Then there’s a competition between James and Victoria, but this time Dragon draw 1 for Victoria and 0 for James. Lastly a competition between James and Digo is held, but this time Dragon really dislike the competition and give zeroes for each of them. Finally we know the score for each one: James–0, Victoria–1, Penghu–1, Digo–1. All except James are the Winner!

However, Dragon’s mom comes back home again and close the TV, driving Dragon to his homework, and find out the paper with scores of all competitors. Dragon’s mom wants to know how many competition Dragon watched, but it’s hard through the paper. Here comes the problem for you, given the scores of all competitors, at least how many competitions had Dragon watched?

Input

The first line of input contains only one integer T(<=10), the number of test cases. Following T blocks, each block describe one test case.

For each test case, the first line contains only one integers N(<=100000), which means the number of competitors. Then a line contains N integers (a1,a2,a3,…,an).ai(<=1000000) means the score of i-th competitor.

Output

Each output should occupy one line. Each line should start with “Case #i: “, with i implying the case number. Then for each case just puts a line with one integer, implying the competition at least should be watched by dragon.

Sample Input

1

3

2 3 4

Sample Output

Case #1: 5

source

HDU4974

题意

熊孩子在家偷看比赛,每场比赛两个人对战,结束后熊孩子可以一名选手加1分,给两名选手加1分,或者不加分,记在纸上,之后他妈回来了。。。想知道熊孩子最少看了几场比赛。

题解

对于一个有分数的选手,我们希望他尽可能的和有分数的选手匹配,对于每次求解,比赛数目最小也要大于等于分数最高的选手的分数,于是从分数最高的选手开始贪心。对于当前剩余的分数最高的选手,我们希望他与其他选手中分数最高的进行匹配,如果匹配分数较低的选手,匹配完成后肯能还会有分数很高的选手,这样的贪心不是最优的,如果与分数搞的选手匹配,剩余的选手分数不会更大,这样的贪心能得到最优解。于是我们维护一个优先队列,将所有选手的分数入队,之后每次从队列中取出最大的两个,把较小的分数加出来,如果两个分数不想等,把他们的差入队,循环这个过程直到队列空。如果最后只从队列取出一个变量,直接把他加到答案上结束循环,输出。AC代码如下

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

int T;
int N,a[100005];
int pre,las,znum;
priority_queue<int> Q;
long long ans;
int x,y;
int main()
{
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
scanf("%d",&N);
while(!Q.empty())
Q.pop();
for(int i=0;i<N;i++)
{
scanf("%d",&a[i]);
if(a[i])
Q.push(a[i]);
}
ans=0;
while(!Q.empty())
{
x=Q.top();Q.pop();
if(!Q.empty())
{
y=Q.top();Q.pop();
ans+=y;
if(x-y)
Q.push(x-y);
}
else
ans+=x;
}
printf("Case #%d: %lld\n",cas,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: