Stones

 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

#include<stdio.h>
#include<queue>
//这个题是真没读懂题目的意思,odd number是奇数,even number是偶数,下次记得了
//就是删除 添加,操作,用优先队列
using namespace std;
typedef struct node
{
    int pos;
    int Max_len;
    int id;
    friend bool operator <(node a,node b)//这里的<可以理解成优先度,大的在后面
    {
        if(a.pos==b.pos)
            return a.Max_len > b.Max_len;
        return a.pos > b.pos;
    }
}Node;
Node stone;

int main()
{
    //简直了,一直超时,我们可以把队列声明放在每个例子的下面啊,这样的话就可以不用初始化了,自己真是。。。
    //很聪明
    //竟然还超时,居然是开辟空间的问题,新开辟空间还要时间么,下次记得不要乱开,要适度
    //本来就是用队列进行排序,没必要开这么大的数组
    //我就不明白这是时间问题,还是空间问题,这个很费时间,但是过了
    int T,n,i;
    priority_queue<Node> pq;
    scanf("%d",&T);
    while(T--)
    {
        while(pq.size())
            pq.pop();
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d %d",&stone.pos,&stone.Max_len);
            pq.push(stone);
        }
        int num=1,ans;
        Node temp;
        if(pq.size()==1)
        {
            ans=pq.top().pos+pq.top().Max_len;
        }
        else
        {
          while(pq.size())
          {
            if(num%2==1)//判定是不是奇数,也可以用(num&1),就是与的符号
            {
                temp=pq.top();
                temp.pos += temp.Max_len;
                ans = temp.pos;
                pq.pop();
                pq.push(temp);
            }
            else
            {
                pq.pop();
            }
            num++;
          }
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值