Stones
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1369 Accepted Submission(s): 855
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
C语言程序代码
/*
题意::
小明在回家的路上玩了一个游戏,当他碰到石头,若为奇数则把它向前扔,若为偶数,则放在原地不动
如果同一地方有很多石头,选择较大的一个(大的扔的近)。问最后没有石头扔时,他距离起点有多远。
解题思路::
这用最优队列解决,可分三步。
1、定义一个结构体来将石头最优排序,若同一位置石头数量有多个,选择扔的最近的在队首。若一个位置只有一个石头
位置小的在队首。
2、 main函数部分,先输入N个石头的位置和能扔的距离,将他们逐个存入队列,队列会根据结构体
将其自动最优排序。
3、判断石头位置是奇是偶,若为奇数,则将石头的位置加上它后面的距离,若为偶数,则将石头位置加1
最后输出石头的位置。
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
struct stone
{
int x,d;
friend bool operator<(stone a,stone b)
{
if(a.x==b.x)//判断同一位置是否有多个石头
return a.d>b.d;//若是,将扔的距离近的放队首。
return a.x>b.x;//若不是,将石头所在位置进的放队首。
}
};
int main()
{
int t,n,i,l;
priority_queue<stone>q;
scanf("%d",&t);
while(t--)
{
stone m;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d",&m.x,&m.d);
q.push(m);
}
l=1;
while(!q.empty())
{
m=q.top();
q.pop();
if(l&1)
{
m.x+=m.d;
q.push(m);
}
l++;
}
printf("%d\n",m.x);
}
return 0;
}