hdu 1896 Stones(栈和队列)

题目大意,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;
}

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

猜你喜欢

转载自blog.csdn.net/cgmm2333/article/details/81255364