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. UVA 1366 九 Martian Mining

    Martian Mining Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  2. CVE-2015-1328(本地提权漏洞)

    /* # Exploit Title: ofs.c - overlayfs local root in ubuntu # Date: 2015-06-15 # Exploit Author: rebe ...

  3. linux的计划任务crontab

    crontab(全称cron table计划任务列表)是一个用于周期性被执行的任的工具. 相关指令: usage: crontab [-u user] file   crontab [ -u user ...

  4. Linux c 下使用getopt()函数

    命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...

  5. 4.mybatis属性和表的列名不相同时的处理方法

    /** * 属性和表的列名不相同时的处理方法 * 1.sql中给列重新命名: * select tid id, tname name from teacher t where tid=#{id} * ...

  6. Python语言精要---上

    下面的记录根据: 麦金尼. 利用Python进行数据分析[M]. 机械工业出版社, 2014. 这本教材的附录部分总结而来   Python的设计特点是重视可读性,简洁性以及明确性 Python不推荐 ...

  7. HDU1518 Square(DFS)

    Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. 数据库mysql中having 和where的区别

    having的用法 having字句可以让我们筛选成组后的各种数据,where字句在聚合前先筛选记录,也就是说作用在group by和having字句前.而 having子句在聚合后对组记录进行筛选. ...

  9. 关于cmbiling.jar cocos2dx的问题

     CMBilling.jar是移动基地的支付库,这样的配置在eclipse下能编译通过,可是用cocos compile命令却找不到这个库及相应的接口函数,移动有个特殊要求,它不允许CMBilling ...

  10. Java I/O 对象序列化

    我们知道对象的持持久化有三种方式: 1: 对象序列化 2: XML 3: 数据库技术 序列化可以帮助使得对象的生命周期不取决与程序是否正在执行,它可以生存于程序的调用之间. 只要将任何对象序列化到单一 ...