HDU1896(优先队列的应用)

来源:互联网 发布:淘宝定制品怎么退款 编辑:程序博客网 时间:2024/06/06 03:02

题目链接

题目描述:

Stones

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


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
221 52 421 56 6
 

Sample Output
1112

解析:

这道题的题意我一开始没有读明白,然后一直在琢磨,为什么第一个样例结果会是11呢?然后读了好几遍,依旧还是没有看懂题目描述的意思,这时候英语的重要性就体现出来了,然后为了不浪费时间,于是我就去网上搜别人的解题告,看看别人对题意的描述,然后才明

白了原来题目中将石头往前面扔,你在往前面走的时候还是会遇到原来扔过的石头,所以不是遇到这个石头之后,就不会再遇到了。因此题意如下:

Sempr在一条直线上从左往右走,在他遇到第奇数块石头时,他会将其往前面扔,能扔多远在输入中会给出,而遇到第偶数个石头时不进行处理。当有

两个石头在同一位置时,则先处理"射程"(能扔的距离最短)的石头,然后Sempr一直往前走,直到前面已经没有任何石头时,这时候计算Sempr与出

发点的距离。

对于样例1的分析:一开始的时候遇到的是第一个石头,他的坐标是1,然后往前扔了5个单位之后,坐标变成6,随后继续往前走,始遇到第二个石头(坐

标是2),忽略,然后继续往前走,又遇到了原来的第一个石头(现在是第三个石头),但是它此时坐标为6,往前扔了5个单位之后,坐标变成11,然后继续往前

走,一直走在坐标11时,这时候他遇到的是第四个石头,因此忽略不计。至此,前面已经没有石头了,因此此时离坐标原点的距离为11。

因此有了以上的分析,我们可以运用优先队列解决上述问题,先将每个石头的位置及其能够扔的距离封装成一个结构体,然后再根据小根堆的思想重载

优先队列的优先级即可。利用一个bool变量来控制奇偶,奇数时处理,偶数时不处理。

完整代码实现:

/**优先队列的应用,注意开始的时候是从第一个石头开始数,然后扔第奇数个石头  *第偶数个石头不动  样例1解释:  一开始的时候遇到的是第一个石头,他的坐标是1,然后往前扔了5个单位之后,坐标变成6  随后继续往前走,开始遇到第二个石头(坐标是2),忽略,然后继续往前走,又遇到了原来的第一个  石头(现在是第三个石头),但是它此时坐标为6,往前扔了5个单位之后,坐标变成11,然后继续往前走  一直走在坐标11时,这时候他遇到的是第四个石头,因此忽略不计。至此,前面已经没有石头了,因此此时  离坐标原点的距离为11  */#include<cstdio>#include<algorithm>#include<queue>using namespace std;typedef long long LL;class Node{public:    LL pos;    LL dis;      //表示能够扔的距离    friend bool operator <(const Node &a,const Node &b){   //必须声明const        if(a.pos==b.pos){            return a.dis > b.dis;        }        return a.pos > b.pos;    }};int main(){    int T,N;    LL P,D;    scanf("%d",&T);    while(T--){        priority_queue <Node> que;        Node node;        scanf("%d",&N);        for(int i = 1;i <= N;++i){            scanf("%I64d %I64d",&node.pos,&node.dis);            que.push(node);        }        bool tmp = true;    //用于切换奇偶        while(!que.empty()){            node = que.top();            que.pop();            if(tmp){                node.pos += node.dis;                que.push(node);       //将扔到远方的石头继续加入队列中            }            tmp = !tmp;        //偶数个不处理        }        printf("%I64d\n",node.pos);    }    return 0;}

 总结:多积累,读题能力很重要,要学会抓关键词,看不懂文章的时候要根据输入输出样例来推断题目所要求的问题。


如有错误,还请指正,O(∩_∩)O谢谢

0 0
原创粉丝点击