HDOJ1896Stones(队列优先级)
2016-07-26 10:14
387 查看
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
题意:在路边踢石头,如果是奇数次遇见石头,则把他踢走,偶数次不管。告诉你石头的位置还有能踢多远,问最远的石头距离出发点多远。如果同时遇见石头,先看到体积大的石头即扔的距离越近体积越大
思路:队列优先级,将扔的距离大的(即体积小的)石头还有所在距离大的放在队列后面。然后所有的数都进队列。判断奇数次遇见石头=踢走(即进队列),偶数次不管。每一次遇见石头都更新最远距离。
代码:
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
题意:在路边踢石头,如果是奇数次遇见石头,则把他踢走,偶数次不管。告诉你石头的位置还有能踢多远,问最远的石头距离出发点多远。如果同时遇见石头,先看到体积大的石头即扔的距离越近体积越大
思路:队列优先级,将扔的距离大的(即体积小的)石头还有所在距离大的放在队列后面。然后所有的数都进队列。判断奇数次遇见石头=踢走(即进队列),偶数次不管。每一次遇见石头都更新最远距离。
代码:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> using namespace std; struct st{ int place; int dis; bool friend operator<(st x,st y) { if(x.place!=y.place) return x.place>y.place; else return x.dis>y.dis; } }a; int main() { priority_queue<st > que; int t,n; cin>>t; while(t--) { int k=1; int max=0; cin>>n; while(n--) { cin>>a.place>>a.dis; que.push(a); } while(!que.empty()) { if(k%2!=0) { a=que.top(); if(a.place>max) max=a.place; que.pop(); a.place+=a.dis; que.push(a); } else { a=que.top(); if(a.place>max) max=a.place; que.pop(); } k++; } cout<<max<<endl; } return 0; }
相关文章推荐
- Log4j 2使用教程
- 基本数据类型与引用数据类型参数传递
- 杭电oj 1096 A+B for Input-Output Practice (VIII)
- github管理代码
- TP开发小技巧
- linux下获取帮助
- 【流媒體】 Android 实时视频编码—H.264硬编码
- centos6.5安装ovs 2.5.0并解决db.sock问题
- 判断循环的用法 if switch
- 更正swiprefreshlayout主动显示小圆圈
- jQuery的deferred对象详解
- 16标准(键盘,控制台)输入输出流
- 排列组合公式
- autocomplete="off"
- @override 下划线
- Postgresql uuid,pgtrashcan,pldebugger install
- 利用路由修改thinkphp框架开发的后台地址
- Svn 的 Update 与Maven 的update project 作用有什么区别
- 讲你肯定能懂的机器学习多维极值求解
- 不使用正则表达式 匹配字符串(字母)