POJ--2674--AAA

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

思路:参考POJ--Ants题目https://blog.csdn.net/queque_heiya/article/details/103786761

关键在于统计输出谁最后掉下来:具体参考代码即可;

注意:输出时用函数floor(),用法参考:https://blog.csdn.net/queque_heiya/article/details/103786761ceil函数和floor函数

//Ants思维题目;
//求最慢走出去那个人所用的时间和对应的名字
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
struct Node{
	char ch[4];//代表这个人走的方向
	double pos;//代表这个人的坐标
	char name[250+10];//代表当前这个人的名字
}a[32000+10];
double p[32000+10];
int main(){
	int n;
	while(1){
		scanf("%d",&n);
		if(n==0)
			break;
		memset(p,0,sizeof(p));
		double dis,v;
		scanf("%lf%lf",&dis,&v);
		for(int i=0;i<n;i++)
			scanf("%s%lf%s",&a[i].ch,&a[i].pos,&a[i].name);
			//cin>>a[i].ch>>a[i].pos>>a[i].name;
		double maxdis=0;
		for(int i=0;i<n;i++){
			if(a[i].ch[0]=='p'||a[i].ch[0]=='P'){
				maxdis=max(maxdis,fabs(dis-a[i].pos));
			}else{
				maxdis=max(maxdis,a[i].pos);
			}
		}
		//找最后的名字
		double DIS=0;
		for(int i=0;i<n;i++){
			if(a[i].ch[0]=='p'||a[i].ch[0]=='P')
				DIS=maxdis;
			else	
				DIS=-maxdis;
			p[i]=DIS+a[i].pos;
		} 
		sort(p,p+n);
		int temp=0;
		/*如果把相撞视为擦肩而过,让蚂蚁们按原方向一直走,
		每只蚂蚁走pos+maxdis或者pos-maxdis后,此时对这些距离从小到大排序,
		找到位置为0或者dis的点,记起下标为temp,由于蚂蚁的相对位置保持不变,则a[temp].name即为所求*/
		for(int i=0;i<n;i++)
			if(p[i]==0||p[i]==dis){
				temp=i;
				break;
			}
		printf("%13.2f %s\n",floor(maxdis/v*100)/100.0,a[temp].name);	
	}
	return 0;
}
发布了150 篇原创文章 · 获赞 73 · 访问量 6600

猜你喜欢

转载自blog.csdn.net/queque_heiya/article/details/104156122
aaa
今日推荐