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

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
#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(优先队列)的更多相关文章

  1. HDU1896Stones(优先队列)

    地址http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目大一比较简单,就是说在一条直线道路上有n个石头,往前走,遇到一个数一个,如果遇到的是第奇数个那就把 ...

  2. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  3. 数据结构:优先队列 基于list实现(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...

  4. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

  5. 数据结构作业——Sanji(优先队列)

    山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...

  6. Java优先队列

    按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...

  7. 优先队列实现Huffman编码

    首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...

  8. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

  9. Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解

    /* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...

随机推荐

  1. hdu 2546 饭卡 (01背包)

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  2. digitalocean纽约机房最先开通IPv6

    DigitalOcean是一家位于美国的云主机服务商,总部位于纽约,成立于2012年.DigitalOcean的服务器全部采用KVM架构,具体高性能处理能力,并且配备SSD固态硬盘,速度优异.每台设备 ...

  3. Myeclipse 激活代码 8.6以前的版本

    public class Akey { private static final String LL = "Decompiling this copyrighted software is ...

  4. 关于socket客户端接收不定长数据的解决方案

    #!/usr/bin/env python3.5 # -*-coding:utf8-*- """ 本实例客户端用于不断接收不定长数据,存储到变量res "&qu ...

  5. app/desktop/view/index.html 显示授权标识

    app/desktop/view/index.html 显示授权标识

  6. c语言-扑克牌小魔术

    /************************************* Copyright(C) 2004-2005 vision,math,NJU. File Name: guess_card ...

  7. Java is Pass-by-Value!

    Java is strictly pass-by-value. which means, when you pass a variable to a method, the method will j ...

  8. read file in a single line

    String text= new Scanner(File('input.txt')).useDelimiter("\\A").next(); java version!

  9. 在Windows上安装Python

    首先,从官网下载 最新版本 的Python 2.7.可通过 Python官网 的”Windows Installer”链接保证下载到的版本是最新的. Windows版本是MSI文件格式,双击它即可开始 ...

  10. robots.txt 文件指南

    http://robots.51240.com/ robots 生成器