HDU1896Stones(优先队列)
Stones
Time Limit : 5000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 37 Accepted Submission(s) : 30
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
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
Sample Input
2
2
1 5
2 4
2
1 5
6 6
Sample Output
11
12
#include <iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std; int i,t,n,k; struct node
{
int p,d;
};
struct cmp
{
bool operator()(node a,node b)
{
if (a.p==b.p) return a.d>b.d;
else return a.p>b.p;
}
}; int main()
{ scanf("%d",&t);
for(;t>;t--)
{
priority_queue<node,vector<node>,cmp> s;
scanf("%d",&n);
node w;
for(i=;i<=n;i++)
{
scanf("%d%d",&w.p,&w.d);
s.push(w);
}
k=;
while(!s.empty())
{
w=s.top();
s.pop();
k++;
if (k%!=)
{
w.p+=w.d;
s.push(w);
}
}
printf("%d\n",w.p);
}
return ;
}
HDU1896Stones(优先队列)的更多相关文章
- HDU1896Stones(优先队列)
地址http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目大一比较简单,就是说在一条直线道路上有n个石头,往前走,遇到一个数一个,如果遇到的是第奇数个那就把 ...
- 堆排序与优先队列——算法导论(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距离最远的城市编号,多个距离相同输出编号 ...
- Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解
/* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...
随机推荐
- LeetCode OJ 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- digitalocean开通德国法兰克福机房,40Gb带宽,支持IPv6
2015年4月15日,vps服务商digitalocean宣布正式开通德国法兰克福机房.5个月前digitalocean宣布筹备Germany机房中心,经过近1个月部署,总算与用户见面了.Frankf ...
- chapter5 函数
在lua中,函数是语句和表达式体现的主要机制.函数可以完成某些特定的任务.计算和返回执行结果. 前者当成一个语句,后者当成一个表达式: *,/) a = ) + ) print(os.date()) ...
- matlab里的nargin
nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能.
- G - Zombie’s Treasure Chest(动态规划专项)
G - Zombie’s Treasure Chest Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- IE6下a标签失效(背景穿透)
background:fixed url(about:blank);有时候做感应区域的时候在ie6下不给背景就会感应不到,比如说当鼠标移到图片的左半边部分,鼠标手势变成向左的箭 头,比如说有些时候a标 ...
- Html 嵌入 swf
1. object + embed 传统的方法 优点:浏览器兼容性好,是 Macromedia 一直以来的官方方法缺点:a.embed 标签是不符合 W3C 的规范的,无法通过验证.当然, ...
- sysadmin_basement
基本部分 1. 系统硬件 1.1. 主板 获取序列号 $ sudo dmidecode -t system |grep -i Serial Serial Number: CNU9032T3N 获取厂商 ...
- iOS UIScrollview代理方法
方法&&属性: // 监控目前滚动的位置(默认CGPointZero) CGPoint contentOffset; - (void)setContentOffset:(CGPoint ...
- mongoDB1--什么是mongoDB
mongodb1.mongodb与其它nosql数据库的区别我们之前应该接触过redis或者memcached,他们属于key-value数据库,他们运用哈希算法关联起来,能够达到快速的查询目的.而m ...