SDUT 3319 优先队列 已知比分求比赛次数

来源:互联网 发布:骗淘宝运费险会封号吗 编辑:程序博客网 时间:2024/04/29 03:36

A simple water problem

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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?

输入

 
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.

输出

 
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.

示例输入

132 3 4

示例输出

Case #1: 5

提示

 本题题意是:已知比赛结果求比赛最少的次数.例如
三个人比赛得分是 2,3,4;每次比赛是两人一场,每人的分是1或0分..

思路.将比分进行从大到小排列,将大的a减去小的b将余下的存入队列,sum+小的分数,表示比了b场.

来源

#include<iostream>#include<cstdio>#include<cstdlib>#include<queue>using namespace std;int main(){int t,T,n;while(~scanf("%d",&T)){int Case=0;while(T--){Case++;priority_queue<int,vector<int>,less<int> >Q;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&t);Q.push(t);}long long sum=0;int a,b;while(!Q.empty()){a=0;b=0;a=Q.top();Q.pop();if(!Q.empty()){b=Q.top();Q.pop();sum+=b;Q.push(a-b);}}sum+=a;printf("Case #%d: %lld\n",Case,sum);}}}


0 0
原创粉丝点击