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. [CF353C]Find Maximum(贪心)

    题目链接:http://codeforces.com/contest/353/problem/C 题意:给你一串数字a[]和一个二进制串,要求找一个不超过m的二进制数,使得与对应a[]上的数字的乘积和 ...

  2. XShell 安装与虚拟机连接

    XShell:是liunx的远程管理工具 为啥要用这个工具呢?因为在古老的liunx字符命令下,是看不到中文的,要么使用liunx的图形化界面(支持中文),要么使用远程管理工具,是在windows中的 ...

  3. SQL语句最基本的性能优化方法

    有些人还不知道sql语句的基本性能优化方法,在此我简单提醒一下,最基本的优化方法:   1.检查是否缺少索引.调试的时候开启“包括实际的执行计划”   执行后会显示缺少的索引,   然后让dba帮助添 ...

  4. ubuntu下卸载vmware

    直接crl+alt+t打开一个terminal,然后输入sudo vmware-installer --uninstall-product vmware-workstation即可卸载!操作如下图: ...

  5. 学习python得到方向与主体

    Python的主体内容大致可以分为以下几个部分: 面向过程.包括基本的表达式,if语句,循环,函数等.如果你有任何一个语言的基础,特别是C语言的基础,这一部分就是分分钟了解下Python规定的事.如果 ...

  6. 在Yii用createUrl中明明白白生成网址

    在Yii中经常要生成URL,不管是为了自动跳转还是仅仅是一个链接.下面对Yii中的URL生成做了一个总结.提示:以下controllerX代表控制器X,actionX代表方法X.在Controller ...

  7. I2总线

    I2C简介 飞利浦(PHILIPS)公司开发的两线式串行总线 每个器件都有一个唯一的地址识别 1.只要求两条总线线路:一条串行数据线SDA,一条串行时钟线SCL 2.每个连接到总线的器件都可以通过唯一 ...

  8. Mysql数据库实践操作之————批量插入数据(100万级别的数据)

    第一种方法:使用insert into 插入 从Redis每次获取100条数据,根据条件去插入到Mysql数据库中: 条件: 如果当前队列中的值大于1000条,则会自动的条用该方法,该方法每次获取从队 ...

  9. 常用sql(转)

    1增 1.1[插入单行]insert [into] <表名> (列名) values (列值)例:insert into Strdents (姓名,性别,出生日期) values ('开心 ...

  10. poj1266Cover an Arc(三角形外接圆)

    链接 求出三角形的外接圆,通过圆心和半径可以知道这个圆的上下左右最远点,分别判断这个四个点跟弧的两端点A,B的关系,假如判断P点,弧内给出点为C,判断PC是否与AB相交即可判断出P是否在弧上. 精度问 ...