Linear world
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 3119 | Accepted: 696 |
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
113.5 2p 3.5 Smarty4 10 1n 3 Joannap 1 Helgan 7 Cleverp 5 Venus0
Sample Output
5.00 Smarty9.00 Venus
题意:在一个线性世界,有n个居民,每个人都在自己的初始位置pos上,运动方向分别为P(正方向)和N(负方向)。 线性世界长度为L,所有居民移动速度都为v。居民们相遇时,就会自动掉头。从初始时刻开始运动,问最后一个掉下去的运动了多长时间,且输出姓名。
题解:运动速度一致,且掉头不花费时间。典型的弹性碰撞模型。我们可以忽略碰撞掉头的影响,将碰撞掉头看成两个居民交换姓名,擦肩而过。 所以最长时间即是离端点最远的居民的运动时间。 哪么谁是最后一个掉下的去的呢?这个要看运动时间最长的居民和哪些居民交换了姓名,最后一个交换的就是最后一个掉下去的。 怎么找到这个人呢?居民们速度一致,所有在前进路上只会与方向相反的居民碰撞。统计出前进路上方向不一致的人数就行了。
注:对弹性碰撞模型不了解的可以参考:POJ1852
代码如下:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
char dir[2];
double pos;
char name[255];
}peo[32200];
double cmp(node a,node b)//将每个人按照位置排序
{
return abs(a.pos)<abs(b.pos);
}
int main()
{
int n,i;
double l,v;
while(scanf("%d",&n)&&n)
{
scanf("%lf%lf",&l,&v);
for(i=0;i<n;++i)
{
scanf("%s%lf%s",peo[i].dir,&peo[i].pos,peo[i].name);
if(peo[i].dir[0]=='n'||peo[i].dir[0]=='N')
peo[i].pos=-peo[i].pos;
}
sort(peo,peo+n,cmp);
double maxn=0.0;
double len;
int id=0;//对应的居民
bool sign;//记录该居民是否向右走
for(i=0;i<n;++i)
{
len=(peo[i].pos<0 ? 0:l) - peo[i].pos;
if(len>maxn)
{
maxn=len;
id=i;
if(peo[i].pos>0)
sign=true;
else
sign=false;
}
}
int cnt=0;//记录路径最长居民前进线上有多少个方向相反的人
if(sign)
{
for(i=id;i<n;++i)
{
if(peo[i].pos<0)
++cnt;
}
id+=cnt;
}
else
{
for(i=id;i>=0;--i)
{
if(peo[i].pos>0)
++cnt;
}
id-=cnt;
}
double ans=maxn/v;//注意输出是右对齐13个空格,并且结果不是四舍五入,是直接截断
printf("%13.2f %s\n",(int)(ans*100)/100.0,peo[id].name);
}
return 0;
}