poj 2674 弹性碰撞

本文介绍了一个关于一维世界中生物移动模拟的问题,探讨了当这些生物相遇时的行为变化及最终消失的时间点。通过设定生物的初始方向、位置和速度,算法能够预测最后一个生物消失的具体时间和名称。

Linear world
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 2746 Accepted: 610

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: 

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


跟蚂蚁的那条题目一样,两个人相遇后交换方向,速度一样,可以直接看作是穿过去了。所以只需要找出方向上最大的距离就可以知道最后一个掉落的时间。

题目还要求输出最后一个掉落的人的name,记录一下需要最长时间的那个位置下标id,只需要知道他行走方向有多少个反方向行走的人记录cnt,id+=cnt就是最后掉落的人。

这个题目还有一些需要注意的,就是输出结果是直接截断到小数点后两位而不是保留两位,因此不能四舍五入。用g++交不知道为什么wa了,用c++交一遍ac

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>

using namespace std;

#define maxn 32005

int dir[maxn];
double pos;
char name[maxn][300];
int n;
double len, v;

int main()
{
    while(~scanf("%d", &n)){
        if(!n) break;
        scanf("%lf %lf", &len, &v);

        char ch;
        int id;
        double t = -1;
        for(int i = 0; i < n; i++){
            ch = getchar();
            while(ch=='\n' || ch ==' ') ch = getchar();

            if(ch=='p' || ch == 'P')
                dir[i] = 1;
            else dir[i] = -1;

            scanf("%lf", &pos);
            scanf("%s", name[i]);

            double tmp;
            if(dir[i]==1)
                tmp = (len-pos);
            else
                tmp = pos;

            if(tmp > t){
                t = tmp;
                id = i;
            }
        }

        int cnt = 0;
        if(dir[id]>0){
            for(int i = id+1; i < n; i++)
            if(dir[i]<0)
            cnt++;
        }
        else{
            for(int i = id-1; i >= 0; i--)
            if(dir[i]>0)
            cnt++;
        }

        if(dir[id]>0)
            id += cnt;
        else
            id -= cnt;

        t = t/v;
        printf("%13.2lf %s\n", (int)(t*100)/100.0, name[id]);


    }

    return 0;
}




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值