hdu 1896.Stones 解题报告

HDU 1896 解题报告
本文解析了HDU 1896题目,通过使用优先队列实现了一个算法来解决奇数次遇到石头才抛掷的问题。详细介绍了如何处理石头的位置和距离,确保位置近的石头和相同位置但距离短的石头优先被处理。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896

题目意思:给出 n 块石头的初始位置和能到达的距离。对于第奇数次遇到的石头才抛掷,偶数次的就忽略。问最多能扔到多远。如果有多颗石头在一个位置,距离小的那个标记为先遇到的。

  所以先解说一下第二个测试案例:

  2

  1  5

  6  6

   

 

      在6这个位置的时候,由于5比6小,所以规定1(5)这个点是先遇上的,是偶数次遇到,所以忽略。

  用优先队列做,位置近的优先级越高,如果位置相同,距离短的优先级越高。

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6 
 7 const int maxn = 100000 + 5;
 8 struct node {
 9     int P, D;
10     bool operator < (const node& a) const {
11         return P > a.P || (P == a.P && D > a.D);
12     }
13 };
14 
15 int main() {
16     #ifndef ONLINE_JUDGE
17         freopen("in.txt", "r", stdin);
18     #endif // ONLINE_JUDGE
19 
20     int T, N;
21     node stone;
22     priority_queue<node> pq;
23     while (scanf("%d", &T) != EOF) {
24         while (T--) {
25             scanf("%d", &N);
26             for (int i = 0; i < N; i++) {
27                 scanf("%d%d", &stone.P, &stone.D);
28                 pq.push(stone);
29             }
30             int ans = 0;
31             int judge = 1;
32             while (!pq.empty()) {
33                 stone = pq.top();
34                 pq.pop();
35                 if (judge) {
36                     stone.P += stone.D;
37                     ans = stone.P;
38                     pq.push(stone);
39                 }
40                 judge = !judge;
41             }
42             printf("%d\n", ans);
43         }
44     }
45     return 0;
46 }

 

转载于:https://www.cnblogs.com/windysai/p/4295343.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值