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 -复制失败 ...
随机推荐
- Struts2_带参数的结果集
页面请求: <a href="user/user?type=1">传参数</a> action: public Integer type; public S ...
- Java—常量和变量
关键字 Java中有特殊用途的词被称为关键字,关键字服务大小写. 标识符 标识符是用于给java程序中的变量.类.方法等命名的符号. 标识符的几条规则: 由字母.数字.下划线(_).美元符号($)组成 ...
- C#设计模式--适配器模式(结构型模式)
一.适配器模式介绍: 适配器模式:将一个类的接口,转换成客户希望的另外一个接口.adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作 例子分析(充电器充电): 模式中的角色: 安 ...
- ownCloud-9.1.1 (Ubuntu 16.04)
平台: Ubuntu 类型: 虚拟机镜像 软件包: owncloud-9.1.1 commercial content management open-source owncloud storage ...
- Spring Boot:内置tomcat启动和外部tomcat部署总结
springboot的web项目的启动主要分为: 一.使用内置tomcat启动 启动方式: 1.IDEA中main函数启动 2.mvn springboot-run 命令 3.java -jar XX ...
- webpack前端构建工具学习总结(一)之webpack安装、创建项目
npm是随nodeJs安装包一起安装的包管理工具,能解决NodeJS代码部署上的很多问题: 常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器 ...
- 支持多域名的免费SSL证书
知乎网友称其支持多域名: https://www.zhihu.com/question/19578422 配置教程: https://www.cnblogs.com/duanweishi/p/8483 ...
- 金庸的武侠世界和SAP的江湖
2018年10月30日晚,成都地铁一号线,Jerry手机app上突然弹出来一条金庸去世的新闻. Jerry识字很早,小学一年级就开始蹭我父亲的<射雕英雄传>看了.小时候,我爸工作的车间里有 ...
- C# 安装 Visual Studio IDE
官网: https://visualstudio.microsoft.com/zh-hans/ 下载社区版(免费的) .微软的软件安装都是很nice的.安装过程中选择需要的配置进行安装(比如.net桌 ...
- 对类参数的序列化和反序列化XML
/// <summary> /// Xml序列化与反序列化 /// </summary> public class XmlUtil { #region 反序列化 /// < ...