[ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]
"One thing is for certain: there is no stopping them; the ants will soon be here. And I, for one, welcome our new insect overlords." |
Kent Brockman
Piotr likes playing with ants. He has n of them on a horizontal pole L cm long. Each ant is facing either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it is facing and wants to calculate where the ants will end up Tseconds from now.
Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containing 3 integers: L , T and n(0 <= n <= 10000) . The next n lines give the locations of the n ants (measured in cm from the left end of the pole) and the direction they are facing (L or R).
Output
For each test case, output one line containing "Case #x:" followed by n lines describing the locations and directions of the n ants in the same format and order as in the input. If two or more ants are at the same location, print "Turning" instead of "L" or "R" for their direction. If an ant falls off the pole beforeT seconds, print "Fell off" for that ant. Print an empty line after each test case.
Sample Input | Sample Output |
2 |
Case #1: |
Problemsetter: Igor Naverniouk
Alternate solutions: Frank Pok Man Chu and Yury Kholondyrev
题目大意:一根长L的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为1/s。当2只蚂蚁相撞时,二者同时掉头(掉头时间忽略不计)。每次给出蚂蚁的初始位置和朝向,计算T秒之后每只蚂蚁的位置。输入第一行为数据组数,每组数据第一行3个整数L,T,n ,以下n行描述一个蚂蚁的初始状态,其中整数x表示其距离木棍左端的距离,字母的初始朝向(L左,R右)
解题思路:从远处看就是一群密密麻麻的点在移动,当蚂蚁因为碰撞而掉头时,看上去和2点"对穿过去"没任何区别,换句话说如果把蚂蚁看成没有区别的小点,那么只需计算每只蚂蚁在T时刻的位置即可。如蚂蚁1=(1,R),蚂蚁2=(3,L),蚂蚁3=(4,L),则2秒钟之后,3只蚂蚁分别为(3,R),(1,L),(2,L)。
掉头等价于穿过是整体而言,对于单个蚂蚁并不是。蚂蚁1的初始状态为(1,R)但是2秒钟之后有蚂蚁状态为(3,R)但不一定是蚂蚁1.换句话说就是要弄清楚哪只是哪只!
又因为每只蚂蚁的相对位置是不变的,因此把所有目标位置从小到大排序,从左到右的每个位置对应初始状态下从左到右的每只蚂蚁。由于原题目中的蚂蚁不一定是按照从左到右的顺序输入,还需要预处理计算输出的第i只蚂蚁的序号order[i].
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = +;
struct Ant{
int id;//输入顺序
int p;//位置
int d;//朝向 -1:L;0:Turning;1:R
bool operator<(const Ant& a)const{
return p<a.p;
}
void set(int ID,int P,int D){
id=ID;p=P;d=D;
}
}before[maxn],after[maxn];
const char dirName[][]={"L","Turning","R"};
int order[maxn];//输入的第i只蚂蚁是终态中的左数第order[i]只蚂蚁
int main(){
int K;
cin>>K;
for(int kase=;kase<=K;kase++){
int L,T,n;
cin>>L>>T>>n;
for(int i=;i<n;i++){
int p,d;
char c;
cin>>p>>c;
d=(c=='L' ? -:);
before[i].set(i,p,d);
after[i].set(,p+T*d,d);
}
//计算order数组
sort(before,before+n);
for(int i=;i<n;i++)
order[before[i].id]=i;
//计算终态
sort(after,after+n);
for(int i=;i<n-;i++)//修改碰撞中的,蚂蚁的方向
if(after[i].p==after[i+].p)
after[i].d=after[i+].d=;
//输出结果
printf("Case #%d:\n",kase);
for(int i=;i<n;i++){
int a=order[i];
if(after[a].p< || after[a].p>L)printf("Fell off\n");
else printf("%d %s\n",after[a].p,dirName[after[a].d+]);
}
printf("\n");
}return ;
}
[ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]的更多相关文章
- cogs 1456. [UVa 10881,Piotr's Ants]蚂蚁
1456. [UVa 10881,Piotr's Ants]蚂蚁 ★ 输入文件:Ants.in 输出文件:Ants.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述 ...
- Uva 10881 Piotr’s Ants 蚂蚁
一根长度为 L 厘米的木棍上有 n 只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为 1 厘米/秒.当两只蚂蚁相撞时,二者同时调头(掉头用的时间忽略不计).给出每只蚂蚁的初始位置和朝向,计算 T 秒之后 ...
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...
- UVA.10881 Piotr's Ants (思维题)
UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...
- 思维题 UVA 10881 Piotr's Ants
题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...
- UVA 10881 Piotr's Ants(等效变换 sort结构体排序)
Piotr's AntsTime Limit: 2 seconds Piotr likes playing with ants. He has n of them on a horizontal po ...
- UVA 10881 - Piotr's Ants【模拟+思维】
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 10881 Piotr's Ants(模拟)
题目链接:https://vjudge.net/problem/UVA-10881 其实这道题的关键只有一句话: 当两个蚂蚁因碰撞而掉头的时候,我们完全可以认为是两个点对穿而过. 这时候我们的主要任务 ...
- UVa 10881 Piotr's Ants (等价变换)
题意:一个长度为L的木棍上有n个蚂蚁,每只蚂蚁要么向左,要么向右,速度为1,当两只蚂蚁相撞时, 它们同时掉头.给定每只蚂蚁初始位置和朝向,问T秒后,每只蚂蚁的状态. 析:刚看到这个题时,一点思路也没有 ...
随机推荐
- CSS 知识汇总
1: inline-block 元素 IE6 7下只有 inline 的元素有 inline-block, 比如 span元素,如果要使其它元素有 inline-block,比如 div 有 in ...
- 解决IE上登陆oracle OEM时报:“证书错误,导航已阻止”的错误
今天在IE上登陆OEM时,报证书错误,导航已阻止,我选择:继续浏览此网站(不推荐),但是点了之后还没有反应,在网上搜了很多,原因基本都是windows的问题,最后发现问题是:oracle oem证书的 ...
- iOS出现<object returned empty description>的解决方法
iOS出现<object returned empty description>的解决方法: 使用 [str length] <= 0 判断处理
- Jquery 生成时钟
$(function(){ showTime(); }): function showTime () { var curtime=new Date(); $(".getDateTime&qu ...
- 缓存大全(Memcached、redis、RabbitMQ )
Memcached: 简介.安装.使用 python操作Memcached Memcached天生支持集群 Redis: 简介.安装.使用.实例 Python操作Redis String.Hash.L ...
- python的编码问题
本文简单介绍了各种常用的字符编码的特点,并介绍了在python2.x中如何与编码问题作战 :) 请注意本文关于Python的内容仅适用于2.x,3.x中str和unicode有翻天覆地的变化,请查阅其 ...
- iOS工作笔记(十四)
1.scrollview的frame指的是其可视范围,contentSize指的是其滚动范围,分别是在水平方向和竖直方向上的 所以要让scrollview在水平方向不能滚动,那么需要如下设置 _scr ...
- 【随笔】mOnOwall添加端口映射
mOnOwall是一个完整的嵌入防火墙软件包,当与一台嵌入个人电脑一起使用时,在免费使用自由软件的基础上,提供具备商业防火墙所有重要特性(包括易用). 这里通过配置mOnOwall的端口设置映射功能, ...
- github 项目版本控制
1.申请github账号 2.安装github for windows工具 安装后就可以使用Git Bash打开特制的终端,在里面用来命令行了.喜欢Git命令行方式的朋友到这里就够了. 打开Git B ...
- android里R.layout.的问题
今天,在Exlipse里的一个项目在.java文件里写 setContentView(R.layout.activity_problem);时,显示错误,以为是R.java文件里没有对应的activ ...