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>






目录
相关文章
|
算法 Java 人工智能
|
C++
HDU1862
中文题,题意挺好理解,不过多赘述。
1268 0