HDU 4974 Dracula and Ethan 优先队列

Dracula and Ethan

Time Limit: 1 Sec  Memory Limit: 256 MB

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 (a 1,a 2,a 3,...,a n).a i(<=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

HINT

题意:

比赛结果有三种 1,0,

0,0

1,1

给出最后得分情况,问你最少举行多少次比赛可以得到最后结果,

题解:

用优先队列,每次尽量使结果保持1,1;

代码:

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <set>
 8 #include <vector>
 9 #include <sstream>
10 #include <queue>
11 #include <typeinfo>
12 #include <fstream>
13 #include <map>
14 #include <stack>
15 typedef __int64 ll;
16 using namespace std;
17 const int inf = (int)1E9+10;
18 inline ll read()
19 {
20     ll x=0,f=1;
21     char ch=getchar();
22     while(ch<‘0‘||ch>‘9‘)
23     {
24         if(ch==‘-‘)f=-1;
25         ch=getchar();
26     }
27     while(ch>=‘0‘&&ch<=‘9‘)
28     {
29         x=x*10+ch-‘0‘;
30         ch=getchar();
31     }
32     return x*f;
33 }
34
35 //*******************************
36
37 struct ss
38 {
39     int x;
40     friend bool operator < (ss s1,ss s2)
41     {
42         return s1.x<s2.x;
43     }
44 };
45 priority_queue< ss >q;
46 int main()
47 {
48
49     int T;
50     cin>>T;
51     int n;
52     int oo=1;
53     while(T--)
54     {
55         scanf("%d",&n);
56         for(int i=1;i<=n;i++)
57         {
58            int x=read();
59            ss xx;
60            xx.x=x;
61            q.push(xx);
62         }
63         ll ans=0;
64         while(!q.empty())
65         {
66             ss a,b;
67             a=q.top();
68             q.pop();
69             b=q.top();
70             q.pop();
71             ans+=(b.x);
72             b.x=(a.x-b.x);
73             //printf("%d\n",b.x);
74             if(b.x!=0)q.push(b);
75             if(q.size()<=2)
76             {
77                 b=q.top();
78                 q.pop();
79                // printf("%d %d\n",b.x,ans);
80                 ans+=(b.x);
81                 break;
82             }
83         }
84         while(!q.empty())q.pop();
85         printf("Case #%d: ",oo++);
86         printf("%I64d\n",ans);
87     }
88
89     return 0;
90 }
时间: 2024-10-13 01:45:53

HDU 4974 Dracula and Ethan 优先队列的相关文章

hdu 4974 A simple water problem(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974 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

HDU 4974 A simple water problem(贪心)

HDU 4974 A simple water problem 题目链接 签到题,很容易贪心得到答案是(sum + 1) / 2和ai最大值的最大值 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 100005; typedef long long ll; int t, n; ll a, Max, sum; int main(

HDU - 4974 A simple water problem

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

2014多校第十场1004 || HDU 4974 A simple water problem

题目链接 题意 : n支队伍,每场两个队伍表演,有可能两个队伍都得一分,也可能其中一个队伍一分,也可能都是0分,每个队伍将参加的场次得到的分数加起来,给你每个队伍最终得分,让你计算至少表演了几场. 思路 : ans = max(maxx,(sum+1)/2) :其实想想就可以,如果所有得分中最大值没有和的一半大,那就是队伍中一半一半对打,否则的话最大的那个就都包了. 1 #include <cstdio> 2 #include <cstring> 3 #include <st

HDU 4974 A simple water problem 模拟(水

水题. #include <cstdio> #include <iostream> #include <queue> #include <algorithm> using namespace std; typedef long long ll; priority_queue<int> q; int main() { int T, cas = 0; scanf("%d", &T); while(T-- > 0) {

2014多校联合十(HDU 4972 HDU 4973 HDU 4974 HDU 4975)

HDU 4972 A simple dynamic programming problem 题意:篮球比赛有1.2.3分球  现给出两队的分差序列(5:3 分差2  3:5分差也是2)  问有多少种可能的比分 思路: 比较简单的想法题  可以类一张表"从分差x到分差y一共有几种情况"  很容易发现只有1->2和2->1的时候会多一种情况  其他均是一种  所以只需要统计这种特殊分差即可  注意一下最后结果要不要乘2  如果最后分差是0就不用因为x:x只有一种  但是最后分差

HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

题目地址:HDU 1428 先用BFS+优先队列求出所有点到机房的最短距离,然后用记忆化搜索去搜. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #incl

hdu 4857 逃生 拓扑排序+优先队列,逆向处理

hdu4857 逃生 题目是求拓扑排序,但不是按照字典序最小输出,而是要使较小的数排在最前面. 一开始的错误思路:给每个点确定一个优先级(该点所能到达的最小的点),然后用拓扑排序+优先对列正向处理,正向输出.这是错误的,如下样例: 1 5 4 5 2 4 3 2 1 3 1 正确的解法:是反向建边,点大的优先级高,用拓扑排序+优先队列,逆向输出序列即可. 根据每对限制,可确定拓扑序列,但此时的拓扑序列可能有多个(没有之间关系的点的顺序不定).本题要求较小的点排到前面,则可确定序列. (1)如果点

HDU - 3790 最短路径问题(Dijkstra+优先队列优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 题意:中文题(边被赋予两种属性,一种是路径,一种是花费),然后略.(逃...... 今天看了卿学姐的视频,初尝SPFA和Dijkstra. 一个是用队列优化,一个是用优先队列优化.这道题目用这两种方法都可以. dijkstra算法思想(贪心):从距离起点最近的点开始,从这个点遍历一遍它周围的点,进行松弛操作,直到最终点. 整个的算法思想就是贪心,每次都给它形成最短路. 这道题目值得注意的是预处