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
",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
",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
",ans);
87     }
88 
89     return 0;
90 }
原文地址:https://www.cnblogs.com/zxhl/p/4690222.html