HDU-1896(Stones)

题目

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回家的一条路上有一些石子,每个石子都由两个数据表示,x,y。x表示石子距离原点的距离,y表示石子能被Sempr丢出的距离(当然y越小石子越大(Sempr的力气是固定的))
Sempr从原点开始走碰到第奇数个石子就丢出,碰到第偶数个不动。
求最远的石子距离原点的距离并输出
输入一开始是一个t表示后面有t组数据,每组数据一开始输入n,表示石头的个数,后面n行x,y对

解题思路

主要是用优先队列或者其他容器进行存储自定义排序…
直接上代码吧!

AC代码

#include<cstdio>
#include<queue>
using namespace std;
struct node
{
    int x;
    int y;
};
struct cmp
{
    bool operator()(node &a,node &b)
    {
        if(a.x!=b.x)
            return a.x>b.x;
        else
            return a.y>b.y;
    }
};
int main()
{
    int t,n;
    int ans;
    int w;//奇偶
    priority_queue<node,vector<node>,cmp> s;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        w=0;
        node v;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%d %d",&v.x,&v.y);
            s.push(v);
        }
        while(!s.empty())
        {
            w++;
            node v;
            v=s.top();
            ans=v.x;
            s.pop();
            if(w%2==1)
            {
                v.x=v.x+v.y;
                s.push(v);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43984169/article/details/87627225