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. Matlab - 矩阵元素引用

    >> A = [ ; ; ] A = 1. 选择第m行n列的元素 >> A(,) ans = 2. 选择第i列所有元素 >> A(:,) ans = 3. 选择第j ...

  2. Openjudge-计算概论(A)-找和为K的两个元素

    描述: 在一个长度为n(n < 1000)的整数序列中,判断是否存在某两个元素之和为k. 输入第一行输入序列的长度n和k,用空格分开.第二行输入序列中的n个整数,用空格分开.输出如果存在某两个元 ...

  3. sharepoint2010配置个人网站的offical方法 来自Jetluning的专栏

    Configuring My Site in SharePoint 201   SharePoint My Sites are commonly referred to as “Facebook fo ...

  4. WinSnap屏幕截图 V4.5.6 官方最新版

    软件名称: WinSnap屏幕截图软件语言: 多国语言授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 2.7MB图片预览: 软件简介:WinS ...

  5. 10.按要求编写Java应用程序。 (1)创建一个叫做People的类: 属性:姓名、年龄、性别、身高 行为:说话、计算加法、改名 编写能为所有属性赋值的构造方法; (2)创建主类: 创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80; 让该对象调用成员方法: 说出“你好!” 计算23+45的值 将名字改为“李四”

    package com.hanqi.test; public class People { private String name,sex; private int age; private doub ...

  6. linux命令积累(Ubuntu)

    1.查看IP地址 ifconfig 2.退出more 使用ctrl+c 3.vi编辑,删除一行为dd 4.ubuntu安装tftp服务器:http://www.cnblogs.com/geneil/a ...

  7. 【3】docker命令集

    root@xcc-VirtualBox:/home/xcc# docker --helpUsage: docker [OPTIONS] COMMAND [arg...]       docker [ ...

  8. PhpMyAdmin管理SQLSERVER的问题

    由于项目需要对MSSQL做管理(多个版本的) 之前有个websqladmin的一个开源的项目,但是感觉功能太弱了,而且,采用的SQLDMO的方式,有点过时了. 所以考虑对其升级,方案大概有几种: 1. ...

  9. ReactiveCocoa常用方法

    //1 代替kvo [[self.redView rac_valuesForKeyPath:@"frame" observer:nil] subscribeNext:^(id x) ...

  10. Chapter 2 Open Book——4

    I walked to Biology with more confidence when, by the end of lunch, he still hadn't showed. 经过中午饭之后我 ...