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距离最远的城市编号,多个距离相同输出编号 ...
随机推荐
- static在C和C++中的用法和区别
static主要有三个作用: (1)局部静态变量 (2)外部静态变量/函数 (3)静态数据成员/成员函数 前两种C和C++都有,第三种仅在C++中有,下面分别作以下介绍: 一.局部静态变量 在C/C+ ...
- Python 脚本帮你找出微信上删除了你的“好友“
- ActivityManager
android.app.ActivityManager 这个类主要用来管理全部设备上的Activities. 权限:android.permission.GET_TASKS 方法:| 返回类型 ...
- 《UNIX环境高级编程》笔记--read函数,write函数,lseek函数
1.read函数 调用read函数从文件去读数据,函数定义如下: #include <unistd.h> ssize_t read(int filedes, void* buff, siz ...
- ASP.NET - 使用 Eval() 绑定数据时使用 三元运算符
ASP.NET邦定数据“<%#Eval("Sex")%>”运用三元运算符: <%#(Eval("Sex", "{0}") ...
- TestNg JAVA 自动化单元测试框架Demo
TestNg TestNg 是java的一个自动化单元测试框架 参考:http://testng.org/doc/index.html 环境准备 既然是java 的自动化单元测试框架,就必须要有jav ...
- java中怎么判断一个字符串中包含某个字符或字符串
public static void main(String[] args) { String str="ABC_001"; ){ System.out.println(" ...
- nginx的反向代理和负载均衡的区别是什么 - 开源中国社区
nginx的反向代理和负载均衡的区别是什么 - 开源中国社区 nginx的反向代理和负载均衡的区别是什么 #user nobody;worker_processes 1;#error_log ...
- POJ训练计划3041_Asteroids(二分图/最小点覆盖=最大匹配)
解题报告 http://blog.csdn.net/juncoder/article/details/38135053 题目传送门 题意: 给出NxN的矩阵,有M个点是障碍 每次仅仅能删除一行或者一列 ...
- Appium 出现 > error: com.test/.activity1 never started. Current: com.test/.activity2
在apium 运行的时候,会出现提示 activity 没有启动,当前是 activity 出现原因: 出现这种情况是因为 launch activity 再启动 app 之后 无法找到,变成了app ...