hdoj 1896 Stones

Stones

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

Total Submission(s): 1474    Accepted Submission(s): 921

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<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
struct line//定义一个结构体,分别存储石头现在的位置和能扔出去的距离到优先队列中!
{
	int x;
	int dis;
	friend bool operator<(line a,line b)//然后每次取“最小的”! 要特别注意多个石头的x一样的情况,要优先考虑y值最小的那块石头
	{
		if(a.x ==b.x )
		return a.dis >b.dis ;
		return a.x >b.x ;
	}
};
int main()
{
	int m,n;
	priority_queue<line > Q;
	scanf("%d",&n);
	line temp;
	while(n--)
	{
		scanf("%d",&m);
	    while(m--)
		{
			scanf("%d%d",&temp.x ,&temp.dis );
		     Q.push(temp);
		}
		int sum=1;
		while(!Q.empty() )//然后每次取“最小的”,如果取得是偶数个就不动,取得是奇数个就要更新该石头的位置并重新存到优先队列中,直到队列空,输出最后一个石头的位置
		{
		temp=Q.top() ;
		Q.pop() ;
		if(sum%2)
		{
			temp.x =temp.x +temp.dis ;
			Q.push(temp); 

		}sum++;
		}
		printf("%d\n",temp.x );
	}
	return 0;
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 14:22:11

hdoj 1896 Stones的相关文章

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 Problem Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every

hdu 1896.Stones 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目意思:给出 n 块石头的初始位置和能到达的距离.对于第奇数次遇到的石头才抛掷,偶数次的就忽略.问最多能扔到多远.如果有多颗石头在一个位置,距离小的那个标记为先遇到的. 所以先解说一下第二个测试案例: 2 1  5 6  6 在6这个位置的时候,由于5比6小,所以规定1(5)这个点是先遇上的,是偶数次遇到,所以忽略. 用优先队列做,位置近的优先级越高,如果位置相同,距离短的优先级越高. 1

HDU 1896 Stones (优先队列)

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

hdu 杭电1896 Stones【优先队列】

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

HDU 1896 Stones

还是优先队列 #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define maxn 100010 struct Node { int x,y,id; friend bool operator < (Node a,Node b) { if(a.x != b.x) return a.x > b.x; else if

hdoj 4004 The Frog&#39;s Games(二分)

The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 5676    Accepted Submission(s): 2732 Problem Description The annual Games in frogs' kingdom started again. The most famous game i

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1