Problem D
Piotr's Ants
Time Limit: 2 seconds

"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 T seconds from now.

Input
The first line of input gives the number of cases, NN 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 before Tseconds, print "Fell off" for that ant. Print an empty line after each test case.

Sample Input Sample Output
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Case #1:
2 Turning
6 R
2 Turning
Fell off Case #2:
3 L
6 R
10 R

Problemsetter: Igor Naverniouk
Alternate solutions: Frank Pok Man Chu and Yury Kholondyrev

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
struct ants
{
int id ; //输入序号
int pos ; //在小木棍上的顺序
int status ; //状态
bool operator <( const ants an) const
{
return pos<an.pos;
}
}begin[maxn],end[maxn]; int order[maxn]; int main()
{
int test;
int T,L,n;
char str;
int a;
scanf("%d",&test);
for(int k=;k<=test;k++)
{
printf("Case #%d:\n",k);
scanf("%d%d%d",&L,&T,&n);
for(int j=;j<n;j++)
{
scanf("%d %c",&a,&str);
int value=(str=='L')?-:;
begin[k]=(ants){j,a,value};
end[k]=(ants){,a+T*value,value};
}
sort(begin,begin+n);
for(int i=;i<n;i++)
order[begin[i].id]=i;
sort(end,end+n);
for(int i=;i<n-;i++)
if(end[i].pos==end[i+].pos)
end[i].status=end[i+].status=;
char chastr[][]={"L","Turning","R"};
for(int i=;i<n;i++){
int a=order[i];
if(end[a].pos<||end[a].pos>L)
printf("Fell off\n");
else
printf("%d %s\n",end[a].pos,chastr[end[a].status+]);
}
printf("\n");
}
return ;
}

Uva---10881 Piotr's Ants(蚂蚁)的更多相关文章

  1. cogs 1456. [UVa 10881,Piotr's Ants]蚂蚁

    1456. [UVa 10881,Piotr's Ants]蚂蚁 ★   输入文件:Ants.in   输出文件:Ants.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述 ...

  2. [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 ...

  3. Uva 10881 Piotr’s Ants 蚂蚁

    一根长度为 L 厘米的木棍上有 n 只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为 1 厘米/秒.当两只蚂蚁相撞时,二者同时调头(掉头用的时间忽略不计).给出每只蚂蚁的初始位置和朝向,计算 T 秒之后 ...

  4. POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题

    两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...

  5. UVA.10881 Piotr's Ants (思维题)

    UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...

  6. 思维题 UVA 10881 Piotr's Ants

    题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...

  7. 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 ...

  8. UVA 10881 - Piotr's Ants【模拟+思维】

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. uva 10881 Piotr's Ants 解题报告

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&pa ...

  10. uva 10881 - Piotr's Ants

    这个题的突破点就在于蚂蚁不能够穿过对方,故相对位置不变: 另外,又可以把蚂蚁看成运动方向不变: 代码: #include<cstdio> #include<algorithm> ...

随机推荐

  1. [转][Unreal3教程]引擎使用操作入门教程

    前言 如果你想转载这篇文章,请不要把这篇文章的来源去掉:http://blog.sina.com.cn/zjmjeo 一来是对我辛苦写这篇文章肯定,二来我可以有更多机会和你们这些朋友交流 1.打开引擎 ...

  2. 【转载】Visual Studio 2015 for Linux更好地支持Linux下的开发

    原文:Visual Studio 2015 for Linux更好地支持Linux下的开发 英文原文:Targeting Linux Made Easier in Visual Studio 2015 ...

  3. PCL点云库:ICP算法

    ICP(Iterative Closest Point迭代最近点)算法是一种点集对点集配准方法.在VTK.PCL.MRPT.MeshLab等C++库或软件中都有实现,可以参见维基百科中的ICP Alg ...

  4. [CF738B]Spotlights(前缀和,模拟)

    题目链接:http://codeforces.com/contest/738/problem/B 题意:问多少个0的方向,使得方向上至少有一个1. 四个方向统计一遍前缀和,向上向左正着记,向下向右倒着 ...

  5. 07.常用的SQL语句

    常用SQL语句,仿照着写(只改动字段和表名,库名) 查看数据库引擎                SHOW ENGINES;(默认InnoDB) 用数据库生成UUID            selec ...

  6. 【面向打野编程】——KMP算法入门

    一.问题 咱们先不管什么KMP,来看看怎么匹配两个字符串. 问题:给定两个字符串,求第二个字符串是否包含于第一个字符串中. 为了具体化,我们以 ABCAXABCABCABX 与 ABCABCABX为例 ...

  7. HDU 2098 分拆素数和

    HDU 2098 分拆素数和 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768K (Java/Others) [题目描述 ...

  8. BSGS模版 a^x=b ( mod c)

    kuangbin的BSGS: c为素数: #define MOD 76543 int hs[MOD],head[MOD],next[MOD],id[MOD],top; void insert(int ...

  9. 访问Google搜索,Google学术镜像搜索

    Google学术镜像搜索:http://dir.scmor.com/google/ 不用FQ也能访问谷歌搜索网站,让我们一起Google 不用FQ也能访问谷歌搜索网站,让我们一起Google(摘自:h ...

  10. jquery动画遮罩

    以前一直以为遮罩都是鼠标移上去,改变透明度实现的,后来看到过这样的一个遮罩动画,然后今天自己写了一个,因为弹出的遮罩是圆形的,所以从美观上来说,这个遮罩效果更适合于方形图片. <div clas ...