POJ 2674 Linear World

本文解析了一道名为Linearworld的编程竞赛题,该题要求模拟一维世界中多个个体的运动过程,最终确定最后一个消失的个体及所需时间。文章提供了问题背景、解题思路与C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

Linear world
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3665 Accepted: 825

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
题意:

弹性碰撞变形,在一条长为l的路上有向前走的与向后走的,求最后一个走出这条路的人及其需要的时间。

方法:

若仅求时间,这就是一道水题。还要求是哪个人,应这么求:若到达终点所需时间最长的那个人A(不考虑改变方向),在他前进方向上有cnt个与他反向前进的人,则实际上最后一个到达终点的人B,既是从A往他前进方向数第cnt个人。

证明略


代码:

//By Sean Chen
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>

#define maxn 50005

using namespace std;

char aname[maxn][300];
vector<double> P,N;
char dir[3];
double ans[maxn];
int main()
{
    int n;
    double d,v;
    scanf("%d",&n);
    while(n)
    {
        scanf("%lf%lf",&d,&v);
        double Max = 0;
        P.clear();
        N.clear();
        double apos;
        for(int i=0;i<n;i++)
        {
            getchar();
            scanf("%s%lf%s",dir,&apos,aname[i]);
            if(dir[0]=='p' || dir[0]=='P')
            {
                P.push_back(apos);
                Max=max(Max,(d-apos)/v);
            }
            else
            {
                N.push_back(apos);
                Max=max(Max,apos/v);                //弹性碰撞,Max记录最大时间
            }
        }
        Max=(int)(Max*100)/100.0;                //保留小数
        double ans1,ans2;
        int id;
        if (P.size() && N.size())
        {
            ans1=(d-P[0])/v;
            ans2=N[N.size()-1]/v;
            if (ans2>ans1)
                id=N.size()-1;
            else
                id=N.size();
        }
        if (P.size() && N.size()==0)
            id=0;
        if (P.size()==0 && N.size())
            id=n-1;

        printf("%13.2lf %s\n",Max,aname[id]);
        scanf("%d",&n);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值