HDU1896Stones(优先队列)

地址http://acm.hdu.edu.cn/showproblem.php?pid=1896

题目大一比较简单,就是说在一条直线道路上有n个石头,往前走,遇到一个数一个,如果遇到的是第奇数个那就把这个石头往前扔距离D[i], 如果是第偶数个,就放置不管。

问遇到的最后一个石头距离出发点的位置是多少。

 

做起来也比较简单就是每遇到第奇数个石头,就将其加上D[i],放回到优先队列(priority_queue)中,然后再去掉一个石头

直接看代码:

 

 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 0x3f3f3f3f
16 #define MAX(a,b) (a > b ? a : b)
17 #define MIN(a,b) (a < b ? a : b)
18 #define mem0(a) memset(a,0,sizeof(a))
19 #define mem1(a) memset(a,-1,sizeof(a))
20 #define lson k<<1, L, mid
21 #define rson k<<1|1, mid+1, R
22 
23 typedef long long LL;
24 const double eps = 1e-12;
25 const int MAXN = 100005;
26 const int MAXM = 500005;
27 
28 struct Node
29 {
30     int P, D;
31 };
32 
33 struct cmp
34 {
35     bool operator () (const Node a, const Node b)
36     {
37         if(a.P != b.P)return a.P > b.P;
38         else return a.D > b.D;
39     }
40 };
41 
42 int T, N;
43 Node stone;
44 
45 int main()
46 {
47     scanf("%d", &T);
48     while(T--)
49     {
50         scanf("%d", &N);
51         priority_queue<Node, vector<Node>, cmp>q;
52         for(int i=0;i<N;i++)
53         {
54             scanf("%d %d", &stone.P, &stone.D);
55             q.push(stone);
56         }
57         int isThrow=1;
58         while(!q.empty())
59         {
60             stone = q.top(); q.pop();
61             if(isThrow)
62             {
63                 stone.P += stone.D;
64                 q.push(stone);
65             }
66             isThrow = !isThrow;
67         }
68         printf("%d\n", stone.P);
69     }
70     return 0;
71 }

 

posted @ 2014-03-01 21:09  再见~雨泉  阅读(850)  评论(0编辑  收藏  举报