hdu1896 Stones

Stones

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


 

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

题意:

这道题其实挺简单的,只是题目有点儿难读懂(英语渣渣的我瑟瑟发抖)

题目说的大概就是小明在路上会遇见一些石头,对于小明遇到的第i个石头,如果i是奇数,小明会把它扔出去di米

如果i是偶数,小明就继续走;现在让我们结合样例来看,对于样例1,小明首先总共会遇到两个石头,第一次(奇数)遇见距离他1米他能扔5米的石头,于是他把这枚石头扔了5米远,这个石头现在具体小明出发时的位置5 + 1 = 6米,小明继续往前走第二次(偶数)遇到了距离他起点2米的石头,他没有扔并继续往前,又第三次(奇数)遇到了距离他起点为6米他能扔5米的石头(其实就是他第一次遇见的那个),于是他把这枚石头又扔了5米,现在这个石头距离起点11米,小明继续往前走并第四次(偶数)遇见了刚才扔的那个石头,小明没有扔,小明继续往前走,然而小明并不会再遇到石头了,结束,综上,样例1最远的石头距离小明的出发点应该是11米;

样例2也是如此

思路:

这题用优先队列再合适不过了,首先用一个结构体存石头距起点的距离和小明能扔的距离,用优先队列存结构体,距离起点越近优先级越高,如果一次性遇见多个石头,则取扔的近的的那个,对于数据的操作则是用一个变量i记录次序,i为奇则扔并把新的放入队列,i为偶则出队;

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define ll long long
using namespace std;
struct stone
{
    int dis,th;
    friend operator <(stone x,stone y)
    {
        if (x.dis == y.dis) return x.th > y.th;
        return x.dis > y.dis;
    }
}a;
int main()
{
    int t,ans;
    scanf("%d",&t);
    while (t --)
    {
        int n;
        priority_queue<stone> q;
        scanf("%d",&n);
        for (int i = 0;i < n;i ++)
        {
            scanf("%d %d",&a.dis,&a.th);
            q.push(a); 
        }
        int i =0;
        while (!q.empty())
        {
            i ++;
            if (i & 1)
            {
                stone b = q.top();
                b.dis += b.th;
                q.pop();
                q.push(b); 
                ans = b.dis;
            }
            else
            {
                q.pop();
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/cloudy_happy/article/details/81237244