zoukankan      html  css  js  c++  java
  • hdu 1896.Stones 解题报告

    题目链接: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
    ", ans);
    43         }
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    ASP.NET MVC的过滤器
    EasyUi 合并单元格占列显示
    Ajax异步请求阻塞情况的解决办法
    AutoFac使用总结
    CodeFirst迁移注意点
    DataTable与实体类互相转换
    mysql中注释的添加修改
    MySql修改数据表的基本操作(DDL操作)
    MySql中的约束
    如何使用单例模式返回一个安全的Connection
  • 原文地址:https://www.cnblogs.com/windysai/p/4295343.html
Copyright © 2011-2022 走看看