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
  1. 2
  2. 10 1 4
  3. 1 R
  4. 5 R
  5. 3 L
  6. 10 R
  7. 10 2 3
  8. 4 R
  9. 5 L
  10. 8 R
  1. Case #1:
  2. 2 Turning
  3. 6 R
  4. 2 Turning
  5. Fell off
  6.  
  7. Case #2:
  8. 3 L
  9. 6 R
  10. 10 R

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

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<algorithm>
  6. #include<vector>
  7. using namespace std;
  8. const int maxn=;
  9. struct ants
  10. {
  11. int id ; //输入序号
  12. int pos ; //在小木棍上的顺序
  13. int status ; //状态
  14. bool operator <( const ants an) const
  15. {
  16. return pos<an.pos;
  17. }
  18. }begin[maxn],end[maxn];
  19.  
  20. int order[maxn];
  21.  
  22. int main()
  23. {
  24. int test;
  25. int T,L,n;
  26. char str;
  27. int a;
  28. scanf("%d",&test);
  29. for(int k=;k<=test;k++)
  30. {
  31. printf("Case #%d:\n",k);
  32. scanf("%d%d%d",&L,&T,&n);
  33. for(int j=;j<n;j++)
  34. {
  35. scanf("%d %c",&a,&str);
  36. int value=(str=='L')?-:;
  37. begin[k]=(ants){j,a,value};
  38. end[k]=(ants){,a+T*value,value};
  39. }
  40. sort(begin,begin+n);
  41. for(int i=;i<n;i++)
  42. order[begin[i].id]=i;
  43. sort(end,end+n);
  44. for(int i=;i<n-;i++)
  45. if(end[i].pos==end[i+].pos)
  46. end[i].status=end[i+].status=;
  47. char chastr[][]={"L","Turning","R"};
  48. for(int i=;i<n;i++){
  49. int a=order[i];
  50. if(end[a].pos<||end[a].pos>L)
  51. printf("Fell off\n");
  52. else
  53. printf("%d %s\n",end[a].pos,chastr[end[a].status+]);
  54. }
  55. printf("\n");
  56. }
  57. return ;
  58. }

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. Walkthrough: Arranging Controls on Windows Forms Using Snaplines

    https://msdn.microsoft.com/en-us/library/t5b5kc41(v=vs.110).aspx Spacing and Aligning Controls Using ...

  2. Ubuntu Linux 下文件名乱码(无效的编码)的快速解决办法

    文件是在WIndows 下创建的,Windows 的文件名中文编码默认为GBK,而Linux中默认文件名编码为UTF8,由于编码不一致所以导致了文件名乱码的问题,解决这个问题需要对文件名进行转码.文件 ...

  3. 【转】在网页中运行VB6程序

      用VB6做的程序在网页里运行, 需要把程序做成OCX格式,下面简单做一介绍: 首先新建一个工程, 选择ActivX控件: 然后添加控件和代码: 然后F5运行 然后按下图设置,去掉弹出消息阻止 这样 ...

  4. [SAP ABAP开发技术总结]ALV

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

  5. SQL SERVER分区表

    简介 分区表是在SQL SERVER2005之后的版本引入的特性.这个特性允许把逻辑上的一个表在物理上分为很多部分.而对于SQL SERVER2005之前版本,所谓的分区表仅仅是分布式视图,也就是多个 ...

  6. Eclipse 高亮显示选中的相同变量

    问题描述: 在 eclipse 中使用快捷键或其他原因,不小心按错了,使得变量的高亮显示没了. 1.网友解决方法:  选择:windows-> preferences->java-> ...

  7. Jquery动画方法 jquery.animate()

    目前在学习Oracle数据库,由于刚接触,学校让练习练习HTML内容,就想起了老师以前提起过的animate方法 animate是jquery的一个方法,这个方法主要功能是能实现比较平滑的动态效果,所 ...

  8. iOS - OC NSFileManager 文件管理

    前言 @interface NSFileManager : NSObject @interface NSFileHandle : NSObject <NSSecureCoding> NSF ...

  9. CSS规范之BFC & IFC

    元素是文档结构的基础,在CSS中,每个元素生成了一个包含了元素内容的框(box,也译为"盒子").但是不同的元素显示的方式会有所不同,例如<div>和<span& ...

  10. jquery+ajax(用ajax.dll)实现无刷新分页

    利用ajax.dll那种方式的无刷新,在这就不说了,新朋友可以看下我的另一片文件http://www.cnblogs.com/dachuang/p/3654632.html 首先,这里用的是jquer ...