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. [Unity3D]开发视图中的标记 - Gizmos

    这个类用来做自己的组件很不错,比如下面这个图的路径点,他其实是个Empty Object,可以自己加脚本让他带上标记.官方解释还可以用来做帮助提示的-.- 大游戏场景的制作时候,你可以用这个在地图上写 ...

  2. json \u unicode字符串转化 c++

    CString GetUStr(const string & str) { std::string showname = str;//\u6211\u7231\u5317\u4eac\u592 ...

  3. [SAP ABAP开发技术总结]FTP到文件服务器,服务器上文件读写

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. NoSQL系列:选择合适的数据库

    NoSQL系列:选择合适的数据库 为什么使用NoSQL数据库? 阻抗失衡 关系模型和内存中的数据结构不匹配 采用更为方便的数据交互方式提升开发效率 待处理的数据量很大 数据量超过关系型数据库的承载能力 ...

  5. JS学习笔记(三) 对象

    参考资料: 1. http://www.w3school.com.cn/js/js_objects.asp ☂ 知识点: ☞ Javascript中的所有事物都是对象. ☞ Javascript是基于 ...

  6. HDU1045 Fire Net(DFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)  ...

  7. DOS命令解释程序的编写

    实验一.DOS命令解释程序的编写 专业:物联网工程   姓名:黄淼  学号:201306104145 一. 实验目的 (1)认识DOS: (2)掌握命令解释程序的原理: (3)掌握简单的DOS调用方法 ...

  8. 初识Oracle

    登录时无法登陆:   无监听程序 ———>启动服务OracleXETNListener   监听程序当前无法识别连接描述中的请求服务————>重启服务       OracleServic ...

  9. Spring读书笔记-----Spring的Bean之Bean的基本概念

    从前面我们知道Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...

  10. CSU 1802 小X的战斗力【拓扑dp】

    题目链接 题意:n个人,每个人有一个能力值.给出m组关系A, B, 表示A的能力值大于B的能力值. 问:m组关系中是否有自相矛盾的?若不矛盾,问:第1个人在所有人的能力值中排名第几?有多少人的能力值的 ...