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

直接优先队列模拟

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; typedef pair<int,int>mp; struct cmp{
bool operator()(mp a,mp b){//first表示位置,second表示距离
if(a.first == b.first)return a.second>b.second;//距离从小到大排序
return a.first>b.first;//位置从小到大排序
}
}; priority_queue<mp,vector<mp>,cmp>oq; int main(){//优先队列插入复杂度logN
int t,n,a,b;
cin>>t;
while(t--){
cin>>n;
while(!oq.empty())oq.pop();
for(int i=0;i<n;++i){
cin>>a>>b;
oq.push(mp(a,b));
}
int num=1;
mp next;
while(!oq.empty()){
next=oq.top();
oq.pop();
if(num&1)oq.push(mp(next.first+next.second,next.second));
++num;
}
printf("%d\n",next.first);
}
return 0;
}

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. cocos2dx 在windows上实现键盘输入

    cocos2d主要面向的是触摸屏幕设备的,在WINDOWS下的定位感觉多多少少就是相当于一个模拟器,因此并没有太多的PC下重要的键盘支持.然而响应键盘消息对于调试来说可以提供不少方便.下边就通过更改c ...

  2. loadrunner参数化总结

    Select next row:Sequential,Rondom,Unique Update value on:Each iteration,Each occurrence,Once 下面分别对这两 ...

  3. eclipse中使用maven插件的时候,运行run as maven build的时候报错

    -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a ...

  4. 基于visual Studio2013解决面试题之1004最长等差数列

     题目

  5. __autoload()方法

    php中__autoload()方法详解 PHP在魔术函数__autoload()方法出现以前,如果你要在一个程序文件中实例化100个对象,那么你必须用include或者require包含进来100个 ...

  6. JCL学习

    JCL基本概念 定义:job control language 用户与操作系统的接口,用户通过JCL语句按照自己的意图来控制作业的执行. JOB的概念:把大机要实现的每一项任务,称为一个JOB或作业. ...

  7. pssh,pscp,pslurp使用实践

    因为常常须要到几十台机器上运行同样的命令,而眼下机器上还没有部署Saltstack或Puppet等集群化管理工具. 因为每台server上都使用同样的公钥,故之前都是写一些脚本:把IP 放到一个文件里 ...

  8. EasyUI - Menu 菜单

    效果: html代码: <div id="mm" class="easyui-menu"> <div id =">New< ...

  9. CSS中position详解与常见应用实现

    在web前台开发时候,我们必不可少的会用到postion属性进行布局定位.今天总结了一下position知识点,与常用功能与大家分享,欢迎大家交流指正. 首先我们对postion属性进行详解. 在CS ...

  10. Qt for Android 部署流程分析

    原地址:http://blog.csdn.net/foruok/article/details/17796017 今天为了测试使用 Qt Creator 3.0.0 开发的纯 C 工程,利用了 Win ...