hdu 1896 Stones

Stones

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

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
 

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

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

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

参考代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

struct node
{
    int pi;
    int di;
    bool operator<(const node t) const
    {
        if(pi!=t.pi)
        return pi>t.pi;
        else  return di>t.di;
    }
};
int main()
{
    node temp,temp2;
  int t,n,m,s;
  scanf("%d",&t);
  while(t--)
  {
      priority_queue<node> que;
      m=1;
      scanf("%d",&n);
      while(n--)
      {
          scanf("%d %d",&temp.pi,&temp.di);
          que.push(temp);
      }
      while(!que.empty())
      {
          if(m%2==0)
            que.pop();
          else
          {
            temp2.pi=que.top().pi+que.top().di;
            temp2.di=que.top().di;
            que.pop();
            que.push(temp2);
          }
          s=que.top().pi;
          m++;
      }
      printf("%d\n",s);
  }
  return 0;
}
原文地址:https://www.cnblogs.com/yly921712230/p/3024007.html