Stones

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 4165    Accepted Submission(s): 2699

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个石子,每个石子的位置和能丢出去的距离已经给了出来。如果石子是在第奇数个,将该位置的最重的石子扔出去(如果只有一个石子,不需要选;如果有多个石子,将丢出去距离最近的那个扔出去)。问最后距离起点最远的石子距离起点有多远。

思路

定义一个结构体的优先队列,按照题目要求设置优先级,将题目中已知的石子的位置和丢出去的距离放入优先队列中。

然后拿出来优先队列中的最上面的石子,如果该石子在奇数位置,进行移动后重新加入到队列中;如果是偶数,不用管他。用一个数maxx记录石子到达的最远距离,当队列为空的时候,停止操作,输出maxx

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
struct wzy
{
int place,dis;
}s;
bool operator < (const wzy &a,const wzy &b)
{
if(a.place==b.place)
return a.dis>b.dis;
else
return a.place>b.place;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int t;
int n;
int p,d;
cin>>t;
while(t--)
{
priority_queue<wzy>que;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>p>>d;
s.place=p;
s.dis=d;
que.push(s);
}
int maxx=0;
for(int i=1;;i++)
{
if(que.empty())
break;
s=que.top();
que.pop();
if(i%2)
{
s.place+=s.dis;
maxx=max(s.place,maxx);
que.push(s);
}
}
cout<<maxx<<endl;
}
return 0;
}

HDU 1896:Stones(优先队列)的更多相关文章

  1. HDU 1896 Stones (优先队列)

    Problem Description Because of the wrong status of the bicycle, Sempr begin to walk east to west eve ...

  2. HDU 1896 Stones --优先队列+搜索

    一直向前搜..做法有点像模拟.但是要用到出队入队,有点像搜索. 代码: #include <iostream> #include <cstdio> #include <c ...

  3. HDU 1896 Stones (优先队列)

    Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  4. HDU 1896 Stones(优先队列)

    还是优先队列 #include<iostream> #include<cstdio> #include<cstring> #include<queue> ...

  5. hdu 1896.Stones 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目意思:给出 n 块石头的初始位置和能到达的距离.对于第奇数次遇到的石头才抛掷,偶数次的就忽略 ...

  6. hdu 1509 & hdu 1873 & hdu 1896 (基础优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1509 裸的优先队列的应用,输入PUT的时候输入名字,值和优先值进队列,输入GRT的时候输出优先值小的名字和对应的 ...

  7. Stones HDU 1896

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目大意: 有n个石头,每个石头有:p  它所在的位置 ,d  它能扔多远 从0 开始,遇到第奇 ...

  8. hdoj 1896 Stones【优先队列】

    Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  9. E - Stones 优先队列

    来源1896 Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning an ...

随机推荐

  1. LeetCode--104--二叉树的最大深度

    问题描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...

  2. 3-19(晚) require_relative 和 require. === operator的解释。

    kernel#require_relative Ruby tries to load the library named string relative to the requiring file's ...

  3. Confluence 6 从外部目录中同步数据如何工作

    下面是有关缓存功能的一些摘要信息: 用户和用户组的缓存信息保存在应用程序的数据库中. 当你连接一个新的外部目录到系统中的时候,一个同步任务将会启动被,并且在后台运行拷贝所有需要的用户和用户组信息,以及 ...

  4. Java基础-面向对象(08)

    面向过程 完成一个需求的步骤:首先是搞清楚我们要做什么,然后在分析怎么做,最后我们再代码体现.一步一步去实现,而具体的每一步都需要我们去实现和操作.这些步骤相互调用和协作,完成我们的需求.面向过程开发 ...

  5. Python在七牛云平台的应用(二)图片瘦身

    (一)七牛云平台的图片瘦身功能简介:(引用自官网) 针对jpeg.png格式图片 瘦身后分辨率不变,格式不变. 肉眼画质不变. 图片体积大幅减少,节省 CDN 流量 官网给的图片压缩率很高,官网给的「 ...

  6. python-day16--内置函数

    内置函数操作 #!usr/bin/env python # -*- coding:utf-8 -*- # 1.locals()和globals() # def func(): # x=1 # y=2 ...

  7. jquery添加类

    一.addClass() 方法向被选元素添加一个或多个类. 1.对P元素添加一个intro类.<!DOCTYPE html><html><head lang=" ...

  8. 手动安装Silverlight 4 Tools for Visual Studio 2010

    手动安装吧,将Silverlight 4 Tools for Visual Studio 2010.exe改成rar文件,解压缩,按照下面的步骤安装: 1.silverlight_developer. ...

  9. Import Data from *.xlsx file to DB Table through OAF page(转)

    Use  Poi.jar Import Data from *.xlsx file to DB Table through OAF page Use Jxl.jar Import Data from ...

  10. PHP:第一章——按位运算和求余运算(判断奇偶数)

    <?php //按位运算:与1按位运算等于0,输出偶数.如果等于1,输出奇数 //输出偶数: for($i=0;$i<10;$i++){ if(($i & 1)==0){ echo ...