题目分类:优先队列+STL

作者:ACShiryu

做题时间:2011-7-18

Stones

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

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

题目大意是路上有很多石头,当你遇到奇数序列的石头就把他向前仍,偶数的不动他,如果两个石头一起,先考虑可以仍的比较近的石头仍也就是比较大的石头,这样一直下去,直到前面所有的石头都不可以仍了为止,求最远的石头距离起点多少题目这题用优先队列非常方便.

分析;可以定义一个结构体,分别存储石头现在的位置和能能出去的距离到优先队列中,然后每次取“最小的”,如果取得是偶数个就不动,取得是奇数个就要更新该石头的位置并重新存到优先队列中,直到队列空,输出最后一个石头的位置

数据分析:程序的时间复杂度是O(nlogn),数据量最大为100,000,不会超时。要特别注意多个石头的x一样的情况,要优先考虑y值最小的那块石头

参考代码:

 1 #include<iostream> 2 #include<cstdlib> 3 #include<cstdio> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 using namespace std; 9 struct Stone{10     int x;    //石头的初始地11     int y;    //石头能扔的最远距离12 };13 bool operator<( Stone a, Stone b )14 { //重载小于,按照结构体中x小的在队顶,如果x一样,则按照y的最小的在//队顶15     if( a.x== b.x ) return a.y > b.y;  16     return a.x > b.x;   17 }  18 int main()19 {20     int t;21     scanf("%d",&t);//测试数据个数22     while(t--)23     {24         int n;25         int i ;26         priority_queue<Stone>que;     //定义一个Stone成员的优先//队列27         scanf("%d",&n);28         Stone tmp;29         for(i =0;i< n ; i++ )30         {31             scanf("%d%d",&tmp.x,&tmp.y);32             que.push(tmp);//入队33         }34         int sum =1;//判断碰到的是第几个石头的标记35         while(!que.empty())//当队列为空就跳出循环,也就是说再//向前就没有石头可以遇到36         {37         tmp = que.top();//去队顶元素,也就是在后面的所有//石头中第一个碰到的石头38             que.pop();//出对39             if(sum%2)40             {//如果是奇数号石头,则处理,否则不做处理41                 tmp . x+=tmp.y;//则向前扔y单位长度42                 que.push(tmp);//扔出去的石头入队43             }44             sum++;//石头计数+145         }46         printf("%d\n",tmp.x);//打印最后一块石头的坐标就是所求//的最远距离47     }48     return 0;49 }

  

转载于:https://www.cnblogs.com/ACShiryu/archive/2011/07/18/2109973.html

HDOJ 1896 Stones 解题报告相关推荐

  1. HDOJ 1896 Stones

    Stones Time Limit : 5000/3000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submis ...

  2. Pangu and Stones 解题报告

    题目: 描述 In Chinese mythology, Pangu is the first living being and the creator of the sky and the eart ...

  3. HDOJ 4005-The war解题报告

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 题意是说,在一个无向带权图中可以在原来没有直接相连的两个点上加一条边,然后要求你删去一条边,要求得出 ...

  4. uscao 线段树成段更新操作及Lazy思想(POJ3468解题报告)

    线段树成段更新操作及Lazy思想(POJ3468解题报告) 标签: treequerybuildn2cstruct 2011-11-03 20:37 5756人阅读 评论(0) 收藏 举报  分类: ...

  5. 解题报告(十八)数论题目泛做(Codeforces 难度:2000 ~ 3000 + )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  6. 【解题报告系列】超高质量题单 + 题解(ACM / OI)超高质量题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我新写的超高质量的题解和代码,题目难度不 ...

  7. 解题报告(三)多项式求值与插值(拉格朗日插值)(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  8. 解题报告(十三)中国剩余定理(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  9. 解题报告(四)生成函数(ACM/ OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

最新文章

  1. 又有多所高校延迟寒假开学
  2. antd option宽度自适应_Web移动端实现自适应缩放界面的方法汇总
  3. update和saveOrUpdate详解
  4. rabbitmq java实例_RabbitMQ消息队列入门篇(环境配置+Java实例+基础概念)
  5. 通过设置rowcount,从Sybase数据库中分页取数
  6. Mysql HA实现MYSQL的高可用(上)
  7. Python爬取某旅游网站中的中国城市信息
  8. 清理apache共享内存引起的oracle宕机
  9. mysql Insert on duplicate引发的死锁
  10. 通过Xcode断点集成 reveal(2017-10-20更新)
  11. RANSAC算法注记
  12. JAVA打印变量类型
  13. openCVPracticalExercise学习笔记01
  14. 深入C++的new(2011-11-15 15:08 )
  15. Acrobat Pro DC 教程,如何填写并签署 PDF 表格?
  16. nginx配置http访问自动跳转到https
  17. Windows 10 使用Easy Sysprep V5 新版封装软件的图文封装教程
  18. 更改手机或者电视的hosts文件
  19. 持久化内存+傲腾持久化内存
  20. Cobaltstrike Socks 代理隧道

热门文章

  1. 命令行下使用curl,采集数据遇到的问题。
  2. C# 反射机制(转)
  3. mvcc原理_Mysql MVCC实现原理
  4. c语言float二进制输出代码_下面C语言中这十四大谜题,不看答案你能做出来吗?...
  5. *p++和*(p++)的区别_同是华为顶级旗舰,P系和Mate系谁最值得购买?明白这点很重要!...
  6. 深度解析,教你如何打造自动驾驶的数据闭环
  7. boost跨平台 c++_跨平台C++整数类型 之一 固定宽度整数(boost和C++11)
  8. 基于nbu oj c语言答案,Just oj 2018 C语言程序设计竞赛(高级组)F:Star(结构体排序+最小生成树)...
  9. 《javaScript100例|04》自动播放——Js幻灯片缓冲效果
  10. oracle 库存管理系统,库存管理系统