HDU-1896-Stones

简介: HDU-1896-Stones


Stones

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1380    Accepted Submission(s): 862


Problem Description

Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning and walk back every evening. Walking may cause a little tired, so Sempr always play some games this time.
There are many stones on the road, when he meet a stone, he will throw it ahead as far as possible if it is the odd stone he meet, or leave it where it was if it is the even stone. Now give you some informations about the stones on the road, you are to tell me the distance from the start point to the farthest stone after Sempr walk by. Please pay attention that if two or more stones stay at the same position, you will meet the larger one(the one with the smallest Di, as described in the Input) first.

 


Input

In the first line, there is an Integer T(1<=T<=10), which means the test cases in the input file. Then followed by T test cases.

For each test case, I will give you an Integer N(0<N<=100,000) in the first line, which means the number of stones on the road. Then followed by N lines and there are two integers Pi(0<=Pi<=100,000) and Di(0<=Di<=1,000) in the line, which means the position of the i-th stone and how far Sempr can throw it.

 


Output

Just output one line for one test case, as described in the Description.

 


Sample Input

          2 2 1 5 2 4 2 1 5 6 6          

 


Sample Output

          11 12          


Sample Output

11 12


题目大意,Sempr往前走,遇到序数(遇见的第n个石头,序数就是n)为奇数的就往前投,偶数的不投.如果石头在同一位置就先考虑比较轻的(投的距离近的)。输入每个石头的初始位置和能投的距离,输出最后一个石头的位置。

例子说明:第一个2是指两个例子:(1)第一个例子共有2个石头。第一个石头在位置1,能投的距离为5;第二个石头在位置2,能投的距离为4。第一个石头序号为奇数,需投,投到了位置6;第二个石头序号为偶,不需投;Sempr继续往前走,刚才的第一个石头序号变为了3,为奇数需要投掷,投到了11处;Sempr还继续往前走,序号为3的石头又变为了序号4,为偶,不需投掷。前面已经没有石头了,结束!(2)第二个例子思路一样,就是要注意当两个石头在同一个位置是先考虑投的近的石头 (比如 第一个石头在位置1,能投的距离为5;第二个石头在位置6,能投的距离为6.第一个石头序号为奇数,需投,投到了位置6,正好第二个石头的位置也是6,但是所能投的距离是6,,比5大所以位置6的第二个石头变为上一个位置投来的石头,原来的石子变为第三个石头),如果序号为偶,再考虑投的远一点的石头!

这个题要用优先队列来处理比较方便。

<span style="font-size:18px;">#include<cstring>
#include<queue>
using namespace std;
struct qu
{
  int pi;
  int di;
   friend bool operator < (qu x,qu y) //const
  {
    if(x.pi!=y.pi)
      return x.pi>y.pi;
      return x.di>y.di; //  此处就是当出现像样例二的情况第一个石子刚好投到第二个石子的位置那么就要看谁// 能投的距离更近了,谁投的近就是第二个//石子,那么大的就变成下一个石子了(此处你只要完全理解了队列的构造就很容易理解了)
  }
};
int main()
{
  int n;
  while(~scanf("%d",&n))
  {
    while(n--)
     {
      int m;
      scanf("%d",&m);
      priority_queue<qu>q;
      int a,b;
      qu data;
      int k=1;<span style="font-family: Arial, Helvetica, sans-serif;">//用来记录第几个石子</span>
      while(m--)
      {
          scanf("%d%d",&a,&b);
          data.pi=a;data.di=b;
          q.push(data);
      }
      while(!q.empty())
      {
        if(k%2==0)  q.pop();// 如果是第偶数个石子的话就不投了,直接删除队首元素
          else
        {
            data.pi=q.top().pi+q.top().di;//如果是第奇数个,需要投出,投出后更新下一//个进栈的数据
            data.di=q.top().di;
            q.pop();
            q.push(data);
        }
        k++;
      }
       printf("%d\n",q.top().pi);
       while(!q.empty())
       {
          q.pop();
       }
     }
  }
  
  return 0;
}</span>






目录
打赏
0
0
0
0
1
分享
相关文章
HDOJ(HDU) 1570 A C
Problem Description Are you excited when you see the title “AC” ? If the answer is YES , AC it ; You must learn these two combination formulas in the school .
795 0
hdu 1892 See you~
点击打开hdu 1892 思路: 二维树状数组 分析: 1 题目给定4种操作:  S x1 y1 x2 y2 询问以(x1 , y1) - (x2 , y2)为对角线的矩形的面积,但是这个对角线不一定是正对角线。
1025 0
HDU1880
题意就是根据咒语查功能,根据功能查看是否存在相应咒语,题意简单,不过是道不错的练习题。         下面的都MLE了,听说C++用G++提交才可以AC,否则也MLE;方法很多,不想做了……         方法一:我用Java的HashMap一直MLE,即便由value反查key减少映射数也一样MLE,听说C++的map可以AC。
1094 0
HDU 2674
  题意:求N!mod2009,N=41时,N!因式分解一定含7*7*41,即N!%2009=0.所以只要计算0
724 0
HDU 1846(巴什博弈)
Brave Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4050    Accepted Submission(s): 2644 Problem Description 十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫《勇敢者的游戏》(英文名称:Zathura),一直到现在,我依然对于电影中的部分电脑特技印象深刻。
802 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等