您的位置:首页 > 其它

HDUOJ 1896(优先队列)

2015-07-29 13:55 363 查看
HDUOJ  1896

 

Stones

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

Total Submission(s): 1397    Accepted Submission(s): 873
[/b]

[align=left]Problem Description[/align]
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.

 
[align=left]Input[/align]
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.

 
[align=left]Output[/align]
Just output one line for one test case, as described in the Description.

 
[align=left]Sample Input[/align]

2
2
1 5
2 4
2
1 5
6 6

 
[align=left]Sample Output[/align]

11
12

题目大意:

        就是说在一条直线道路上有n个石头,往前走,遇到一个数一个,如果遇到的是第奇数个那就把这个石头往前扔距离s, 如果是y遇到的第偶数个石头,就放置不管。

问遇到的最后一个石头距离出发点的位置是多少。

 

/*解题思路:第一个石头(奇数)必定要扔(已排列),因为第一个石头的位置最小(位置小所以排在最前面),扔完后又相当于产生了一个新的石头,

这个新产生的石头的位置等于原来 的位置加上所扔的距离,而第一个石头的数据要清除,新产生的石头的数据要添加上 ,然后接下来的一组石头再按位置

从大到小排序,此时的第一个石头(对列顶部)要 清除,因为此块石头将是Sempr遇到的第二块石头(偶数)所以他不会扔,所以要清除。清除后新的对顶

元素(新的第一个石头此时为Sempr遇到的第三个石头所以要扔)会被扔出去,相当于产生了一个新的石头 ,这个新产生的石头的位置等于原来 的位置加上所扔的距离, 

,因而这个对顶元素被扔出去后,其相关数据要被清除 。现在又得到新的一组石头,此时的第一个石头为 Sempr遇到的第四个石头,所以他不会扔,他会扔这个石头的后面

的一个石头…………,直到最后剩下两个石头,第一个石头为偶数个见到的,所以清除,其后面的石头被扔,产生一个新的石头,此时这块新的石头将是唯一一个剩下的石头

且是 Sempr遇到的第偶数个石头,不会扔,而此时这块石头的位置也就是 Sempr从起点到最后一个石头的位距离。

         此题重在理解,代码很简单 ,主要用优先队列 (操作对象为结构体) 

*/

My  solution:

/*2015.7.28*/

#include<stdio.h>
#include<queue>
using namespace std;
struct stu
{
int lie;/*石头所在位置*/
int s;/*石头所能扔的距离*/
friend bool operator<(stu a,stu b)
{
if(a.lie!=b.lie)
return a.lie>b.lie;
else     /*注意,题目要求当两个石头在同一个位置时,选择扔的远的石头,这点很容易忽略*/
return a.s>b.s;/*这个是按从小到大排序,因为要选择大的,恰好可以把排在前面的小的清除,保留大的*/
}
}now;
int main()
{
int n,m,i,j,k;
scanf("%d",&n);
for(i=0;i<n;i++)
{
j=0;
priority_queue<stu>p;/*优先队列的定义*/
scanf("%d",&m);
for(j=0;j<m;j++)
{
scanf("%d%d",&now.lie,&now.s);
p.push(now);
}
now=p.top();
now.lie=now.lie+now.s;/*第一个石头扔出去相当于产生一个的新的石头,这是新石头的数据*/
p.pop();     /*第一个石头数据,已经无用了,清除掉*/
p.push(now); /*将新的石头数据添入*/
while(p.size()>1)
{
p.pop();  /*第一个数据将是路人遇到的第偶数个石头,不扔,需清除掉*/
now=p.top();/*路人遇到的第奇数个石头,扔完后,要清除*/
p.pop();
now.lie=now.lie+now.s;
p.push(now); /*记录添加新的石头数据*/
}
printf("%d\n",p.top().lie); /*最后一个石头的位置,就是起点到最远石头的距离*/
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: