POJ:2674-Linear world(名字交换碰撞)

Linear world

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 4514 Accepted: 1025

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

解题心得:

  1. 题意就是有一个木杆长len,给你n个人在木杆上走,每个人有自己的方向和位置,可以向前走也可以向后走,每个人速度相同,如果两个人相撞了那么两个人瞬间掉头以相同的速度反向运行,问最后一个人掉下木杆的时间和那个人的名字。
  2. 和蚂蚁走线一个思路,两个人相撞就可以看做两个人直接从对方身体穿过去了,但是这个题还多了一个两个人互相穿过去之后还交换了名字。那么怎么知道最后一个人的名字是什么呢。可以先算出如果不交换名字最后掉下去的人是谁,然后看在他行走的方向和他方向相反的人有几个,,有多少人就会有多少次碰撞(因为速度一样),假设有k个。因为每次碰撞这个人的名字都只能传给他方向上的前面一个人,所以这个人的名字就向前面传递了k次。
  3. 还有这个题在输入输出上搞事情真的没啥意思。注意P和p都是向前,输出保留两位小数(不是四舍五入),数字要留够十三个数位。

#include <algorithm>
#include <stdio.h>
#include <climits>
using namespace std;
const int maxn = 4e4+100;

struct Inhabitants{
    int dir;
    double pos,time;
    char name[300];

    bool operator < (const Inhabitants& a) const {
        return abs(a.pos) > abs(pos);
    }
}inha[maxn];

int main() {
    int n,ans_man;
    double v,len,Max;
    while(scanf("%d",&n) && n) {
        Max = 0.0;
        scanf("%lf%lf",&len,&v);
        for(int i=0;i<n;i++) {
            char temp[5];
            scanf("%s%lf%s",temp,&inha[i].pos,inha[i].name);
            if(temp[0] == 'p' || temp[0] == 'P')
                inha[i].dir = 1;
            else
                inha[i].dir = 0;
            if(inha[i].dir)
                inha[i].time = (len-inha[i].pos)/v;
            else
                inha[i].time = inha[i].pos/v;
        }
        sort(inha,inha+n);
        for(int i=0;i<n;i++) {
            if(inha[i].time > Max) {
                Max = inha[i].time;
                ans_man = i;
            }
        }
        int cnt = 0;
        if(inha[ans_man].dir) {
            for(int i=ans_man;i<n;i++)
                if(inha[i].dir != inha[ans_man].dir)
                    cnt++;
            ans_man += cnt;
        } else {
            for(int i=ans_man;i>=0;i--)
                if(inha[i].dir != inha[ans_man].dir)
                    cnt++;
            ans_man -= cnt;
        }
        printf("%13.2f %s
",(int)(Max*100)/100.0,inha[ans_man].name);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/GoldenFingers/p/9107103.html