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
解题心得:
- 题意就是有一个木杆长len,给你n个人在木杆上走,每个人有自己的方向和位置,可以向前走也可以向后走,每个人速度相同,如果两个人相撞了那么两个人瞬间掉头以相同的速度反向运行,问最后一个人掉下木杆的时间和那个人的名字。
- 和蚂蚁走线一个思路,两个人相撞就可以看做两个人直接从对方身体穿过去了,但是这个题还多了一个两个人互相穿过去之后还交换了名字。那么怎么知道最后一个人的名字是什么呢。可以先算出如果不交换名字最后掉下去的人是谁,然后看在他行走的方向和他方向相反的人有几个,,有多少人就会有多少次碰撞(因为速度一样),假设有k个。因为每次碰撞这个人的名字都只能传给他方向上的前面一个人,所以这个人的名字就向前面传递了k次。
- 还有这个题在输入输出上搞事情真的没啥意思。注意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\n",(int)(Max*100)/100.0,inha[ans_man].name);
}
return 0;
}
POJ:2674-Linear world(名字交换碰撞)的更多相关文章
- POJ 2674 Linear world
POJ 2674 Linear world 题目大意: 一条线上N只蚂蚁,每只蚂蚁速度固定,方向和坐标不同,碰头后掉头,求最后掉下去那只蚂蚁的时间和名字. 注意两点: 相撞可视为擦肩而过,蚂蚁们不管掉 ...
- POJ 2674 Linear world(弹性碰撞)
Linear world Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4426 Accepted: 1006 Desc ...
- Greedy:Linear world(POJ 2674)
Linear world 题目大意:一些人生活在线性世界中,到达线性世界两端就会消失,两个人的前进方向有两个,相遇会改变各自相遇方向,求最后一个人掉下的人的名字和时间. 其实这一题就是弹性碰撞的模 ...
- POJ 2674
Linear world Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2448 Accepted: 564 Descr ...
- poj 2674 线性世界 弹性碰撞
弹性碰撞的题目一般都是指碰到就会掉转方向的一类题目,这里我们可以忽略掉头,仅仅看成擦肩而过,交换名字等等 题意:一条线上N只蚂蚁,每只蚂蚁速度固定,方向和坐标不同,碰头后掉头,求最后掉下去那只蚂蚁的名 ...
- ProgrammingContestChallengeBook
POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...
- cocos 碰撞系统
前面的话 本文将简要介绍 Cocos Creator 中的碰撞系统,Cocos Creator 内置了一个简单易用的碰撞检测系统,支持圆形.矩形以及多边形相互间的碰撞检测 编辑碰撞组件 当添加了一个碰 ...
- Ubuntu 16.04 64位安装arm-linux-gcc交叉编译器以及samba服务器
交叉编译器是嵌入式开发的必要工具,但是由于目前大多数人使用64位ubuntu,在照着很多教程做的时候,就会失败,失败原因是64位ubuntu需要额外安装32位的兼容包.以arm-linux-gcc-3 ...
- 关于Server2008 R2日志的查看
Server 2008 r2通过 系统事件查看器 分析日志: 查看 系统 事件: 事件ID号: 审计目录服务访问 4934 - Active Directory 对象的属性被复制 4935 -复制失败 ...
随机推荐
- 零基础逆向工程34_Win32_08_线程控制_CONTEXT结构
线程控制 实验 挂起线程 ::SuspendThread(hThread); 恢复线程 ::ResumeThread(hThread); 终止线程 (这里讲了同步调用与异步调用) 方式一: 此方法结束 ...
- (十)JavaScript之【DOM定义】
DOM定义Document Object Model 文档对象模型 是干什么的?改变 HTML 元素的内容 (innerHTML)改变 HTML 元素的样式 (CSS)改变 HTML 元素的属性对 H ...
- Oracle:Start with connect by prior 递归
SELECT * from CONNECT BY {PRIOR列名1=列名2|列名1=PRIOR列名2} [START WITH]; Oracle的递归查询: START WITH :描述开始 ...
- 梦织未来Windows驱动编程 第03课 驱动的编程规范
最近根据梦织未来论坛的驱动教程学习了一下Windows下的驱动编程,做个笔记备忘.这是第03课<驱动的编程规范>. 驱动部分包括基本的驱动卸载函数.驱动打开关闭读取写入操作最简单的分发例程 ...
- ListView、DataGrid 不显示列标题
<!--ListView不显示列标题--> <Style TargetType="{x:Type GridViewColumnHeader}"> <S ...
- 对类参数的序列化和反序列化XML
/// <summary> /// Xml序列化与反序列化 /// </summary> public class XmlUtil { #region 反序列化 /// < ...
- java 使用hashmap一个键对应多值的方法
背景:在你使用map对象时,你可能会有一个key,对应多个值的需求 实现: import java.util.ArrayList; import java.util.HashMap; import j ...
- Veritas NetBackup™ 状态码"十大"常见报错状态码
我在刚开始学习Netbackup的时候,没少走弯路.经常会遇到各种稀奇古怪的 error 信息,遇到报错会很慌张,急需一个解决问题的办法.跟无头苍蝇一样,会不加思索地把错误粘到百度上,希望赶紧查找一下 ...
- Python实现接口测试中的常见四种Post请求数据
前情: 在日常的接口测试工作中,模拟接口请求通常有两种方法, 利用工具来模拟,比如fiddler,postman,poster,soapUI等 利用代码来模拟,使用到一些网络模块,比如HttpClie ...
- vue 中$index $key 已经移除了
https://cn.vuejs.org/v2/guide/migration.html#index-and-key-移除 之前可以这样: 1 2 3 4 5 6 <ul id="ex ...