Linear world
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 3599 | Accepted: 810 |
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.
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.
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
题意:在一个线性的世界中,只有一条直线,直线上有n个人,n个人按一定的速率,按着各自的方向(向前或者向后)走着,如果有两个人碰面,则各自调头转换方向继续沿着当前方向前进,现在给定道路的长度,道路规定的区间为[0,L],出了该区域的人就不能在回到区域,
问最后一个走出该区域的人的名字以及他走出该区域所花的时间。
思路:首先考虑各自走各自的,即碰面不回头,各自都继续沿着原来的方向前进,其实如果每个人都一模一样,这等价于两个人碰面之后掉头继续行走,此时不需要知道最后一个走出的人的名字,但所花的时间可以轻松的得到。现在需要知道最后一个走出区域的人的名字,可以在
原来模型的基础上考虑,假定已经找到所需花费的最长的时间,以及当前花时间最长人的名字,此时这个人与其他某个人碰面了,那么花时间最长人的名字将会被交换成碰面的那个人,以此不断的交换,最终得到最后一个走出区域的人的名字。所以现在只要知道名字会被交换几次
就可以知道最后一个走出区域的人的名字。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<string> #include<cmath> using namespace std; const int N_MAX = 32000 + 4; float L, V; struct people { char c; float dir; string name; float t; bool operator <(const people&b) { return dir < b.dir; } }; people peop[N_MAX]; int main() { int n; while (scanf("%d",&n)&&n) { scanf("%f%f",&L,&V); for (int i = 0; i < n; i++) { cin >> peop[i].c; scanf("%f",&peop[i].dir); cin >> peop[i].name; } sort(peop, peop + n);//这样就会按照初始位置从小到大排列 for (int i = 0; i < n; i++) { if (peop[i].c == 'p' || peop[i].c == 'P') {//朝向正面 peop[i].t = (L - peop[i].dir) / V; } else peop[i].t = peop[i].dir / V; } float time = 0; int pos; bool what; for (int i = 0; i < n; i++) { if (peop[i].t > time) { time = peop[i].t; pos = i;//pos用来记录当前花时间最长的人位置 if (peop[i].c == 'p' || peop[i].c == 'P') what = 1;//如果朝正方向 else what = 0;//如果朝反方向 } } //如果每个人都各自走各自的,peop[pos]是当前走到花时间最长的人 if (what) {//考虑初始位置,在他前面的人有没有反向的,每有一个反向的,碰面之后花时间最长的人的名字就会被交换一次 for (int i = pos + 1; i < n; i++)//统计一下一共会相交几次 if (peop[i].c == 'n' || peop[i].c == 'N') pos++; } else { for (int i = pos - 1; i >= 0; i--) { if (peop[i].c == 'p' || peop[i].c == 'P') pos--; } } printf("%13.2f ",(floor)(time*100)/100); cout << peop[pos].name << endl; } return 0; }