POJ 2674 Linear world (经典题目 -- 这个有坑= =)
来源:互联网 发布:ubuntu 网络配置 编辑:程序博客网 时间:2024/05/17 22:22
题意:
在一条线上,告诉你每个蚂蚁的位置和方向,两只蚂蚁碰面后原路返回,告诉你线的长度,和蚂蚁速度(所有的蚂蚁速度都一样),求最后一只蚂蚁掉落的时间和名字。
思路:
很久之前做的一道思路题目了。 这个题比较坑把 记录一下。
两只蚂蚁碰面后相当于不回头一直往前走。
那么我们可以记录下来每只蚂蚁假如不碰面掉落的时间,取个最大值就是最后一只蚂蚁掉落的时间,在把那个时间对应的每只蚂蚁位置记录下来,还在线上的就是最后一只蚂蚁。
坑:
竟然是截断到两位小数,不是四舍五入,那么直接给ans×100,向下取整后在除以100即可。
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <cstdlib>#include <iostream>#include <vector>#define Siz(x) (int)x.size()using namespace std;const int maxn = 320000 + 7;const double eps = 1e-10;int dcmp(double a,double b){ if (fabs(a-b) < eps) return 0; if (a > b) return 1; return -1;}char s[320];struct Node{ char name[320]; double pos;int dir; void read(){ scanf("%s",s); if (s[0] == 'p' || s[0] == 'P')dir = 1; else dir = -1; scanf("%lf",&pos); scanf("%s",name); }}p[maxn];vector<double>v;int main(){ double L,t; int n; while(~scanf("%d",&n) && n){ scanf("%lf %lf",&L, &t); v.clear(); double Maxtime = -1; for (int i = 0; i < n; ++i){ p[i].read(); double pos = p[i].pos; int dir = p[i].dir; if (dir == 1){ if ((L-pos)/t > Maxtime)Maxtime = (L-pos)/t; } else { if (pos/t >Maxtime)Maxtime=pos/t; } } int ans; for (int i = 0; i < n; ++i){ double pos = p[i].pos; int dir = p[i].dir; double p2 = pos+dir*t*Maxtime; v.push_back(p2); } sort(v.begin(),v.end()); for (int i = 0; i < Siz(v); ++i){ double p2 = v[i]; if (dcmp(p2,0) >= 0 && dcmp(p2,L) <= 0){ ans= i; break; } } Maxtime*=100; Maxtime = (int)Maxtime *1.0; Maxtime/=100; printf("%13.2f %s\n",Maxtime,p[ans].name); } return 0;}/**113.5 2p 3.5 Smarty410 1p 1 Helgan 3 Joannap 5 Venusn 7 Clever0 5.00**/
Linear world
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 3633 Accepted: 813
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
Source
Southeastern Europe 2005
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator
0 0
- POJ 2674 Linear world (经典题目 -- 这个有坑= =)
- poj 2674 Linear world
- poj 2674 Linear world
- POJ 2674 Linear World
- POJ - 2674 Linear world(弹性碰撞)
- POJ 2674 Linear world(弹性碰撞)
- POJ - 2674 Linear world(弹性碰撞)
- POJ 2674 Linear world 已翻译
- 这个题目也是经典的BFS算法,题目的链接如下: http://poj.org/problem?id=1915
- poj 2674 Linear world 弹性碰撞 升级的蚂蚁
- POJ题目分类 (膜拜经典)
- POJ 3040 Allowance(贪心经典题目)
- 经典POJ题目
- 经典POJ题目
- 经典POJ题目
- 经典POJ题目
- 经典POJ题目 (转)
- poj上搜索经典题目
- sql 千万级数据表新建索引
- arc下内存泄漏的解决小技巧
- SwipeRefreshLayout的使用注意事项
- UITabBarItem不能显示文字和图片
- 多线程的实现
- POJ 2674 Linear world (经典题目 -- 这个有坑= =)
- iOS 自动布局详细介绍
- base64字符串与图片的转换
- linux网络编程之Listen函数参数介绍
- centos6.5使用yum安装saltstack-之salt-master salt-minion
- mybaits错误解决:There is no getter for property named 'id' in class 'java.lang.String'
- Spring Boot 学习之环境搭建
- 隧道 之后自己写的
- Android 6.0 boot