hdu--4974--贪心

多校的题 切了5 6题 就这道会点= =

其他都是些什么树的分治 图论的一些东西= =

这题 贪心 不难发现吧 每次都尽量选取各自都能获得1分的方案 但可能会出现1 0这种只能一个人得1分 一个人得0分得的方案。

一开始 我很sb地 wa在任意选取了2人 比较他们得分大小。。

正确姿势当然是要 从大到小的来选取。  然后剩余的继续留着选取

放入个优先队列来实现比较好

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 using namespace std;
 6
 7 priority_queue<int> q;
 8
 9 void init( )
10 {
11     while( !q.empty() )
12         q.pop();
13 }
14
15 int main()
16 {
17     cin.sync_with_stdio(false);
18     int t , n , num , ans;
19     int x , y;
20     cin >> t;
21     for( int k = 1 ; k<=t ; k++ )
22     {
23         cin >> n;
24         init( );
25         ans = 0;
26         for( int i = 1 ; i<=n ; i++ )
27         {
28             cin >> num;
29             q.push(num);
30         }
31         while( !q.empty() )
32         {
33             x = q.top();
34             if( x<=0 )
35                 break;
36             q.pop();
37             if( !q.empty() )
38             {
39                 y = q.top();
40                 q.pop();
41                 if( y<=0 )
42                 {
43                     ans += x;
44                     break;
45                 }
46                 q.push( x-y );
47                 ans += y;
48             }
49             else
50             {
51                 ans += x;
52                 break;
53             }
54         }
55         cout << "Case #" << k << ": " << ans << endl;
56     }
57     return 0;
58 }    

时间: 2024-08-12 08:16:03

hdu--4974--贪心的相关文章

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

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

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 4105 贪心思想

淋漓尽致的贪心思想 波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的. 当在寻找波谷碰到零了就自然当成波谷. 当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到. 代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰. #include<iostream> #include<cstdio> #include<cstring> #inc

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

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只有一种  但是最后分差