hdu1896之优先队列应用
Stones
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 678 Accepted Submission(s): 407
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.
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.
2
1 5
2 4
2
1 5
6 6
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之优先队列应用的更多相关文章
- Hdu1896 Stones(优先队列) 2017-01-17 13:07 40人阅读 评论(0) 收藏
Stones Time Limit : 5000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submis ...
- 贪心+优先队列之更改优先级-hdu1896
题目描述: 题目理解: Sempr从位置0往前走,一路上他会遇到石子,如果这颗石子是他遇到的第奇数颗石子,那么他就把石子往前扔出去,如果他遇到的是第偶数颗石子,他会把它留在原地.需要注意的是,Semp ...
- 堆排序与优先队列——算法导论(7)
1. 预备知识 (1) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
- 数据结构:优先队列 基于list实现(python版)
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...
- python优先队列,队列和栈
打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...
- 数据结构作业——Sanji(优先队列)
山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...
- Java优先队列
按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...
- 优先队列实现Huffman编码
首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...
随机推荐
- 基于visual Studio2013解决面试题之1109全排列
题目
- HBase数据存储格式
好的数据结构,对于检索数据,插入数据的效率就会很高. 常见的数据结构 B+树 根节点和枝节点非常easy,分别记录每一个叶子节点的最小值,并用一个指针指向叶子节点. 叶子节点里每一个键值都指向真正的 ...
- TEXT文本编辑框3 点击按钮添加文本至文本输入框
In this exercise a function that loads the texts of an internal table into the text window, is imple ...
- 窗口绘制有关的消息整理 WM_PAINT, WM_NCPAINT, WM_ERASEBKGND
WM_PAINTWM_PAINT是Windows窗口系统中一条重要的消息,应用程序通过处理该消息实现在窗口上的绘制工作. WM_NCPAINT当窗口客户区以外的部分(如窗口标题栏.菜单栏等)需要需要重 ...
- eclipse发布项目报错:Multiple Contexts hava a path of “/xxx“
你的位置:首页 > Java编程 > eclipse发布项目报错:Multiple Contexts hava a path of “/xxx“ eclipse发布项目报错:Multipl ...
- JavaScript快速入门(六)——DOM
概念扫盲 DOM DOM是 Document Object Model(文档对象模型)的缩写,是W3C(万维网联盟)的标准.DOM 定义了访问 HTML 和 XML 文档的标准:“W3C 文档对象模型 ...
- ajax后台处理返回json值
public ActionForward xsearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, Htt ...
- iOS国际化时遇到的错误:read failed: the data couldn't be read because it isn't in the correct format.
事实上这个错误非常easy,就是当我们在国际化的时候,写key,写着写着就忘了加 ";" 所以查看一下自己的Localization文件就能够了
- Swift - 本地数据的保存与加载(使用NSCoder将对象保存到.plist文件)
下面通过一个例子将联系人数据保存到沙盒的“documents”目录中.(联系人是一个数组集合,内部为自定义对象). 功能如下: 1,点击“保存”将联系人存入userList.plist文件中 2,点击 ...
- 【UVA】12299-RMQ with Shifts(线段树)
改动的时候因为数据非常小,所以能够直接暴力改动,查询的时候利用线段树即可了. 14337858 option=com_onlinejudge&Itemid=8&page=show_pr ...