HDOJ 1896 Stones(优先队列)
2015-07-28 20:30
309 查看
Stones
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1385 Accepted Submission(s): 866
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
题意:某先生自行车坏了,上下班在路上扔石头,从起始位置出发,第奇数个遇到的石头扔,第偶数个遇到的石头不扔。现在知道有n个石头,且知道每个石头距离起始位置的距离,和该先生能把此石头扔的距离。求扔完石头后,距起始位置最远的石头的距离。
解题思路:先将所有的石头信息存入队列。(石头与该先生的距离越短,优先级越高。有多个石头与先生距离相等时,则先生能将石头扔出的距离越短优先级越高) 在依次模拟先生扔石头的过程,被扔出去的石头,重新进入队列时,它距起始位置的距离要再加上被扔出的距离。
代码如下:
#include<cstdio> #include<queue> using namespace std; struct node { int dis,far; }s; bool operator < (const node &x,const node &y) { if(x.dis==y.dis) return x.far>y.far; else return x.dis>y.dis;//注意能被扔出去的距离越短,优先级越高 } int main() { int t,n,pi,di,k; scanf("%d",&t); while(t--) { k=1; priority_queue<node>q; scanf("%d",&n); while(n--) { scanf("%d%d",&pi,&di); s.dis=pi; s.far=di; q.push(s); } k=1;//奇数扔,偶数不扔 while(!q.empty()) { s=q.top(); q.pop(); if(k&1) { s.dis+=s.far; q.push(s); } k++; } printf("%d\n",s.dis); } return 0; }
相关文章推荐
- APP首次启动引导界面和启动界面设置——iOS开发
- 【面试加分项】java异常处理之try_catch_finally
- IOS被遗忘的知识
- OC中 static 与 const 的作用
- Activity里添加ViewGroup ViewGroup里添加ViewGroup
- tinyxml使用笔记与总结 http://www.cppblog.com/elva/archive/2008/04/24/47907.html
- hdu 5319 Painter
- Reorder List
- 判断单链表是否有环以及环的连接点
- 百练2757最长上升子序列
- IndexError: list index out of range的错误原因
- JSP概述
- 渣渣ACM日记——3461-Oulipo(POJ)
- VS中使用TinyXml操作xml文件
- Android ViewPager引导页(三)
- prim最小生成树算法题poj2485
- OpenGL关于glBegin的参数的理解
- I/O复用中,epoll 和select 的区别,以及epoll和select的实例
- 计算机网络读书笔记-----网络层
- Ubuntu 12.04 安装 Apache2+PHP5+MySQL