Stones

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

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个石头的位置pi,和能够扔的距离Di,从左(0位置)往右走,碰到的石头为奇数个就往右扔,碰到的石头为偶数个就跳过,问最后一个石头距离出发点的距离

直接优先队列模拟

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<queue>
  7. #include<algorithm>
  8. #include<map>
  9. #include<iomanip>
  10. #define INF 99999999
  11. using namespace std;
  12.  
  13. typedef pair<int,int>mp;
  14.  
  15. struct cmp{
  16. bool operator()(mp a,mp b){//first表示位置,second表示距离
  17. if(a.first == b.first)return a.second>b.second;//距离从小到大排序
  18. return a.first>b.first;//位置从小到大排序
  19. }
  20. };
  21.  
  22. priority_queue<mp,vector<mp>,cmp>oq;
  23.  
  24. int main(){//优先队列插入复杂度logN
  25. int t,n,a,b;
  26. cin>>t;
  27. while(t--){
  28. cin>>n;
  29. while(!oq.empty())oq.pop();
  30. for(int i=0;i<n;++i){
  31. cin>>a>>b;
  32. oq.push(mp(a,b));
  33. }
  34. int num=1;
  35. mp next;
  36. while(!oq.empty()){
  37. next=oq.top();
  38. oq.pop();
  39. if(num&1)oq.push(mp(next.first+next.second,next.second));
  40. ++num;
  41. }
  42. printf("%d\n",next.first);
  43. }
  44. return 0;
  45. }

hdu1896之优先队列应用的更多相关文章

  1. Hdu1896 Stones(优先队列) 2017-01-17 13:07 40人阅读 评论(0) 收藏

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

  2. 贪心+优先队列之更改优先级-hdu1896

    题目描述: 题目理解: Sempr从位置0往前走,一路上他会遇到石子,如果这颗石子是他遇到的第奇数颗石子,那么他就把石子往前扔出去,如果他遇到的是第偶数颗石子,他会把它留在原地.需要注意的是,Semp ...

  3. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  4. 数据结构:优先队列 基于list实现(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...

  5. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

  6. 数据结构作业——Sanji(优先队列)

    山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...

  7. Java优先队列

    按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...

  8. 优先队列实现Huffman编码

    首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...

  9. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

随机推荐

  1. BZOJ 1112: [POI2008]砖块Klo1112( BST )

    枚举每个长度为k的区间, 然后用平衡树找中位数进行判断, 时间复杂度O(nlogn). 早上起来精神状态不太好...连平衡树都不太会写了...果断去看了会儿番然后就A了哈哈哈 ------------ ...

  2. 删掉SafeDrv病毒(这个病毒有点意思)

    1.手动删除以下文件: %program files%\common files\safedrv.exe %documents and settings%\administrator\rkoxe.dr ...

  3. 基于visual Studio2013解决C语言竞赛题之1017次数

         题目 解决代码及点评 /* 功能:有人说在400, 401, 402, ...499这些数中4这个数字共出现112次,请编程序判定这 种说法是否正确.若正确请打印出'YE ...

  4. attachEvent和addEventListener详解

    attachEvent方法可以动态的为网页内的元素添加一个事件.通常你想为某个按扭添加一个单击事件时.你都会在按扭内写上onclick=事件名称.使用attachEvent则不必这样做.你把写好的事件 ...

  5. Jquery Mobile转场特效之slide | 小小iPhone开发

    Jquery Mobile转场特效之slide | 小小iPhone开发 2012 Jquery Mobile转场特效之slide 作者:小小   发布:2012-12-12 14:03   分类:j ...

  6. HDU 1593 find a way to escape

    数学题. 题意是问你能不能逃脱. 当V1的 角速度大于 V2的时候,能够一直保持 V1,O(圆心),V2 三点一线. 跑到一定距离.角速度小于的时候,就以三点一线为初始状态直接跑直线. #includ ...

  7. Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)

    解题报告 二分图第一题. 题目描写叙述: 为了參加即将召开的会议,A国派出M位代表,B国派出N位代表,(N,M<=1000) 会议召开前,选出K队代表,每对代表必须一个是A国的,一个是B国的; ...

  8. MYSQL 语法大全自己总结的

    mysql语法大全 --------数据链接---------------------数据库服务启动net start mysql --关闭服务net stop mysql --登录 -u,-p后面不 ...

  9. perl 匿名函数传参

    $subref=sub { my $a=shift; return $a; }; print $subref->("xxyyzz");

  10. 基于visual Studio2013解决面试题之0410计算二进制中1的个数

     题目