Piotr's Ants
Time Limit: 2 seconds


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

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

2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R Sample Output
Case #1:
2 Turning
6 R
2 Turning
Fell off Case #2:
3 L
6 R
10 R 题目大意:一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬要么朝右爬,速度为1厘米/秒。当两只蚂蚁相撞时,二者同时掉头(掉头时间忽略不计)。给出每只蚂蚁初始位置和朝向,计算T秒之后每只蚂蚁的位置。 分析:在远处观察蚂蚁运动,由于黑点太小,所以当蚂蚁碰撞掉头时,看上去和两个点“对穿而过”没有任何区别。换句话说,如果把蚂蚁看成没有区别的小点,那么只需独立计算出每只蚂蚁在T时刻的位置即可。比如,3只蚂蚁,蚂蚁1=(3,R),蚂蚁2=(3,L),蚂蚁3=(4,L),则两秒钟之后,3只蚂蚁分别为(3,R),(1,L)和(2,L)。
而且所有蚂蚁的相对顺序保持不变,因此把所有目标位置从小到大排序,则从左到右的每个位置对应于初始状态下从左到右的每只蚂蚁。由于原题中的蚂蚁不一定按照从左到右的顺序输入,还需要预处理计算出输入中的第i只蚂蚁的序号order[i]。 代码如下:
 #include<cstdio>
#include<algorithm>
using namespace std; const int maxn = + ; struct Ant {
int id; // 输入顺序
int p; // 位置
int d; // 朝向。 -1: 左; 0:转身中; 1:右
bool operator < (const Ant& a) const {
return p < a.p;
}
} before[maxn], after[maxn]; const char dirName[][] = {"L", "Turning", "R"}; int order[maxn]; // 输入的第i只蚂蚁是终态中的左数第order[i]只蚂蚁 int main() {
int K;
scanf("%d", &K);
for(int kase = ; kase <= K; kase++) {
int L, T, n;
printf("Case #%d:\n", kase);
scanf("%d%d%d", &L, &T, &n);
for(int i = ; i < n; i++) {
int p, d;
char c;
scanf("%d %c", &p, &c);
d = (c == 'L' ? - : );
before[i] = (Ant){i, p, d};
after[i] = (Ant){, p+T*d, d}; // 这里的id是未知的
} // 计算order数组
sort(before, before+n);
for(int i = ; i < n; i++)
order[before[i].id] = i; // 计算终态
sort(after, after+n);
for(int i = ; i < n-; i++) // 修改碰撞中的蚂蚁的方向
if(after[i].p == after[i+].p) after[i].d = after[i+].d = ; // 输出结果
for(int i = ; i < n; i++) {
int a = order[i];
if(after[a].p < || after[a].p > L) printf("Fell off\n");
else printf("%d %s\n", after[a].p, dirName[after[a].d+]);
}
printf("\n");
}
return ;
}
 

UVA 10881 Piotr's Ants(等效变换 sort结构体排序)的更多相关文章

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

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

  2. 思维题 UVA 10881 Piotr's Ants

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

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

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

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

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

  5. 网上关于sort结构体排序都不完整,我来写一个完整版的 2014-08-09 16:50 60人阅读 评论(0) 收藏

    主要参考sort函数_百度文库, 但是那篇有错误 2.结构体排序,a升,b降,c降 平板视图 打印? 01 #include <iostream> 02 #include <algo ...

  6. 洛谷P1068 分数线划定:sort结构体排序+贪心

    题目描述 世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A市对所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试. 面试分数线根据计划录取人数的150%划定, ...

  7. UVa 10881 Piotr's Ants (等价变换)

    题意:一个长度为L的木棍上有n个蚂蚁,每只蚂蚁要么向左,要么向右,速度为1,当两只蚂蚁相撞时, 它们同时掉头.给定每只蚂蚁初始位置和朝向,问T秒后,每只蚂蚁的状态. 析:刚看到这个题时,一点思路也没有 ...

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

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

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

随机推荐

  1. node系列1

    NodeJS基础 JS是脚本语言,脚本语言都需要一个解析器才能运行,NodeJS就是一个解析器.nodejs.org 打开终端,键入node进入命令交互模式,可以输入一条代码语句后立即执行并显示结果 ...

  2. HW5.18

    public class Solution { public static void main(String[] args) { System.out.printf("%s\t%s\n&qu ...

  3. HW1.3

    public class Solution { public static void main(String[] args) { System.out.println(" J A V V A ...

  4. Codeforces10D–LCIS(区间DP)

    题目大意 给定两个序列,要求你求出最长公共上升子序列 题解 LIS和LCS的合体,YY好久没YY出方程,看了网友的题解,主要是参考aikilis的,直接搬过来好了 经典的动态规划优化. 用opt[i] ...

  5. SRM144 - SRM 148(少144-DIV1-LV3,147-DIV2-LV3)

    SRM 144 DIV 1 500pt tag:组合 题意:彩票中奖.给定n, m,从1-n中选择m个数组成数列a1, a2, a3...am.对于数列{am}分别满足以下条件的概率: (1)数列所有 ...

  6. devexpress中gridcontrol 一些样式改变

    改变footer为扁平化效果 整个footer背景色CustomDrawFootere.Appearance.BackColor = Color.Transparent; e.Appearance.D ...

  7. 教程-Delphi设置功能表

    1.锁定窗体上的控件,禁止移动位置 D7-Edit>Lock Controls 2.设置控件永久显示名字 D7-Tools>Environment Options>Designer& ...

  8. Java异常处理的误区和经验总结

    本文着重介绍了 Java 异常选择和使用中的一些误区,希望各位读者能够熟练掌握异常处理的一些注意点和原则,注意总结和归纳.只有处理好了异常,才能提升开发人员的基本素养,提高系统的健壮性,提升用户体验, ...

  9. 准备开一个地图SDK的开源项目

    最近有点空闲时间了, 准备开一个地图SDK的开源项目, 现在的地图SDK已经有很多了, 再做一个跟重新发明个轮子差不多, 但还想做的原因是想在别的轮子的基础上造个轮子... 初步设想是基于开源的地图渲 ...

  10. solr使用方法 完全匹配

    最近一直被solr的搜索困扰,搜索汉字时不能搜索出自己想要的内容,经过研究和查询发现,问题出在没有完全匹配上,主要还是对solr使用不太熟练. 解决方法:以前UserRealname:某某家长,这样搜 ...