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

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  2. URL参数带中文,后台接收乱码解决方案

    1.前台中文参数用encodeURIComponent()进行编码,如: var textName= encodeURIComponent(name); 2.对整个URL用encodeURI()进行编 ...

  3. Go Slices: usage and internals

    Introduction Go's slice type provides a convenient and efficient means of working with sequences of ...

  4. nyoj 27 水池数目

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  5. 1、IIS常见的的问难及解决方法

    配置IIS 7.5 出现的问题及解决办法: 1.问题:  CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Tempor ...

  6. Microsoft.Net中数字签名技术

    概 述:本文以Microsoft.Net中创建签名的CAB文件为例,说明如何创建签名文件,从而使你的代码能够安全的在INTERNET上公布.希望能起到抛转引玉的作用. keyword:数字签名,Act ...

  7. [Whole Web] [AngularJS + Grunt] Using ng-html2js to Convert Templates into JavaScript

    ng-html2js takes .html templates and converts them into strings stored in AngularJS's template cache ...

  8. Android(java)学习笔记139:在TextView组件中利用Html插入文字或图片

    首先我们看看代码: 1.activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...

  9. Android(java)学习笔记166:Java中输入和输出流概念

     程序在内存中运行,文件在磁盘上,把文件从磁盘上读入内存中来,当然是输入流了,   反之,把内存中的数据写到磁盘上的文件里面去就是输出.通常都是这样的,用了过滤流的情况复杂一些,则另当别论.

  10. Linux启动遇到的问题

    双系统装的Ubuntu,在一次意外关机后无法进入图形界面,每次输入完密码进入桌面后又会退到密码输入界面.使用命令行df -hl查看发现根目录使用率达到100%.推测是因为意外关机导致的,但是找不到应该 ...