hdoj 1896 Stones【优先队列】
Stones
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1363 Accepted Submission(s): 850
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.
题意:每行的两个数据pi和Di分别代表石头所在的位置和所能扔出的距离,当石头是第奇数个是向外扔
当是第偶数个时绕过去,(之前扔过来的石头也参与运算)直至没有石头可扔 求出起点0到终点
的距离:
AC代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
int stone;
int dis;
friend bool operator< (node a,node b)
{
if(a.stone==b.stone)
return a.dis>b.dis;
else
return a.stone>b.stone;
}
};
int main()
{
int n,m,j,i,t;
scanf("%d",&t);
while(t--)
{
priority_queue<node>q;
node x,x1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
int p,d;
scanf("%d%d",&p,&d);
x.stone=p;
x.dis=d;
q.push(x);
}
int ans=0;//判断是第奇数还是第偶数个石头
int sum;//记录总距离
while(!q.empty())
{
ans++;
x=q.top();
sum=x.stone;
if(ans&1)//如果是第奇数个石头则扔出去删除队首元素并
{ //将扔出去后的石头的 坐标 及可以扔出的距离入队
x1.stone=x.stone+x.dis;
x1.dis=x.dis;
q.pop();
q.push(x1);
}
else//第偶数个石头则直接删除队首元素
{
q.pop();
}
}
printf("%d\n",sum);
}
return 0;
}
hdoj 1896 Stones【优先队列】的更多相关文章
- HDU 1896 Stones (优先队列)
Problem Description Because of the wrong status of the bicycle, Sempr begin to walk east to west eve ...
- HDU 1896 Stones --优先队列+搜索
一直向前搜..做法有点像模拟.但是要用到出队入队,有点像搜索. 代码: #include <iostream> #include <cstdio> #include <c ...
- HDU 1896 Stones(优先队列)
还是优先队列 #include<iostream> #include<cstdio> #include<cstring> #include<queue> ...
- HDU 1896 Stones (优先队列)
Stones Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- E - Stones 优先队列
来源1896 Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning an ...
- hdu 1896.Stones 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目意思:给出 n 块石头的初始位置和能到达的距离.对于第奇数次遇到的石头才抛掷,偶数次的就忽略 ...
- hdu 1509 & hdu 1873 & hdu 1896 (基础优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1509 裸的优先队列的应用,输入PUT的时候输入名字,值和优先值进队列,输入GRT的时候输出优先值小的名字和对应的 ...
- Stones 优先队列
Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning and walk ...
- Hdu1896 Stones(优先队列) 2017-01-17 13:07 40人阅读 评论(0) 收藏
Stones Time Limit : 5000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submis ...
随机推荐
- PL/SQL学习(六)触发器
原文参考:http://plsql-tutorial.com/ 创建语法: CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER | IN ...
- wamp介绍
Wamp介绍 Windows下的Apache+Mysql/MariaDB+Perl/PHP/Python,一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在 ...
- PHP 冒泡排序法
<?php // 冒泡排序法:将一个数组中的值按照从小到大的顺 序排序 $arr = array(33, 1, 4, 5, 2, 3, 7, 9, 8, 99); $len = count($a ...
- mongodb数据库操作--备份 还原 导出 导入
首先数据库备份: mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径 mongodump -h 127.0.0.1 -u admin -p ...
- onbeforeunload、beforeunload
<script type="text/javascript"> function addOnBeforeUnload(e) { var ev = e || ev ...
- torch7在mac上的安装
在安装torch7的时候,一开始一直参考的是官方的文档: http://torch.ch/docs/getting-started.html#_ 但是无论怎么装都无法成功,主要是卡在了source那一 ...
- networkRequest
void TPHttp::applyResource(){ QJsonObject json; json.insert("ProductCode","MoVd ...
- 移动App双周版本迭代实战--转载备用
对于移动互联网产品来说,迭代的速度就是生命.我创业时做移动App时是一周一版,而现在是2周1版.相比起小公司,大公司迭代时间虽长,却更为不易,因为大公司流程更多,参与人数更多,需求更多,实现这样的快速 ...
- iPhone不同机型适配 6/6plus --备用
机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了,表示机器屏幕尺寸变大了: 像素:表示屏幕图片的大小,跟坐标之间有个对应关系,比如1:1或1:2等: ppi:代表屏幕物理大小到图片大小的比例值,如果 ...
- git 常用命令行整理
1.创建分支dev git branch dev 2.切换到dev分支 git checkout dev 3.创建并切换本地分支,分支名为dev git checkout -b dev 4.查看本地所 ...