POJ2674-Linear world【弹性碰撞】

原题链接
Linear world
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3483 Accepted: 779
Description

The Disc, being flat, has no real horizon. Any adventurous sailors who get funny ideas from staring at eggs and oranges for too long and set out for the antipodes soon learned that the reason why distant ships sometimes looked as though they were disappearing over the edge of the world was that they were disappearing over the edge of the world. (Terry Pratchett -Colour of Magic)
Not so long time ago people used to believe that they live on 2-D world and if they will travel long enough in one direction, they will fall down over the edge. Even when it was proved that the Earth is rounded some of them were still afraid to travel to the southern hemisphere.
Try to imagine one 1-D (linear) world. On such world there are only two possible directions (left and right). All inhabitants of such world were created exactly at the same time and suddenly all of them start to move (all with same constant velocity) in one or the other direction. If two inhabitants encounter each other, they politely exchange greetings and then they turn around and start to move in an opposite direction. When an inhabitant reaches the end of the world he falls away and disappears.
Your task is to determine, for a given scenario of creation, which inhabitant and when (counting from the moment of creation) will be the last one to fall away. You can assume that the time required to exchange greetings and turn around is 0.
Input

The input consists of multiple descriptions (data sets) of the creation moment. File structure is as follows:
N
LV
DIR POS NAME

The first line defines the number of inhabitants (N<32000). Data set starting with value N=0 represents the end of the input file. The second line contains length of the world L(float) and velocity of inhabitants V(float). Both values are always positive. In next N lines the data about inhabitants are given in an order of increasing POS (positive direction):
DIR – initial direction (‘p’ or ‘P’ for positive and ‘n’ or ‘N’ for negative)
POS – position in the time of creation (0<=POS<=L)
NAME – name of inhabitant (string up to 250 characters)
Input values within one line are separated with at least one space and there will be no empty lines in input. You may assume that input is always correct and that each data set has only one unique solution.
Output

The output consists of one line per each input data set. The first value should be the time when the last inhabitant will fall of the linear world counting from the moment of creation. Value should be printed truncated to two decimal places in a field 13 characters wide. The second value should be the name of the inhabitant. Values should be separated with single space character.
Sample Input

1
13.5 2
p 3.5 Smarty
4
10 1
p 1 Helga
n 3 Joanna
p 5 Venus
n 7 Clever
0
Sample Output

     5.00 Smarty
     9.00 Venus

Source

Southeastern Europe 2005
题意:有一条线段,线段上有n个人,每个人只能向左或向右走,每个人有一个初始的位置,从0开始计时,如果在行走过程中两个人相遇那么就会立刻调转头继续走,直到所有人都走到线段的外面去。求最后一个人走出去所用的时间和这个最后走出去人的名字
思路:此题属于弹性碰撞类的题,这些人相遇其实可以认为是他们穿过了对方。那么最后离开的人所用的时间一定是原本单人的情况下所需要的最长时间。而且最后一定是开始有几个向右边走的,最后也就是最右边的几个向右走。所以我们在确定了消耗时间最长的人之后,再确定一下与他同方向的人中他是排在第几的就行了。这里注意要输出13位,并且不是四舍五入地保留两位小数,而是直接截断式地保留两位小数。所以就需要一些特殊的输入输出技巧。

//http://poj.org/problem?id=2674
#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = (~(0ULL)>>1);
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=32000 + 5;
struct node
{
	char p;//代表这个人走的方向
	double pos;//代表这个人的坐标
	string name;//代表当前这个人的名字
}a[maxn];
bool cmp(node x,node y){
	return x.pos<y.pos;
}
int main(){
		int n;
		while(scanf("%d",&n)==1 && n){
			double L,V,t;
			double maxdis=-1;//最大的需要走的距离
			int maxtimeloc=0;//最后走出去的人在a数组中的下标
			scanf("%lf%lf",&L,&V);
			for(int i=0;i<n;i++){
				cin >> a[i].p >> a[i].pos >> a[i].name;
			}
			//对所有人的位置进行排序
			sort(a,a+n,cmp);//最后发现要不要这步都可以,因为题中本来就已经排好序了
			for(int i=0;i<n;i++){
				if(a[i].p=='p' || a[i].p=='P') t=L - a[i].pos;
				else t=a[i].pos;
				if(maxdis<t){
					maxdis=t;
					maxtimeloc=i;
				}
			}
			//确定这个被标记的人在正向的队伍或者反向队伍中的位置,这个位置上原本的人就是我们要找的人
			//如果他是正向的,那么统计他的后面还有多少个正向的人
			int sum=0;
			//实现13位的保留两位小数(非四舍五入而是直接截断的方法)
			printf("%13.2lf ",(int)(maxdis/V*100)/100.0);
			if(a[maxtimeloc].p=='p' || a[maxtimeloc].p=='P'){
				for(int i=n-1;i>maxtimeloc;i--)
					if(a[i].p=='p' || a[i].p=='P') sum++;
				cout << a[n-1-sum].name << endl;
			}
			//如果他是反向的,那么统计他的前面还有多少个反向的人
			else{
				for(int i=0;i<maxtimeloc;i++)
					if(a[i].p=='n' || a[i].p=='N') sum++;
				cout << a[sum].name << endl;
			}
		}
        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门豪杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值