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

题目意思:有一条 L 厘米长的杆,上面有 n 只蚂蚁,给出每只蚂蚁的朝向以及离杆上最左端的距离,问 T 秒之后每只蚂蚁到达的位置,如果 T 秒后某个位置有多只蚂蚁同时到达,那么这堆蚂蚁处于的位置 + Turning,如果超过这条杆的长度,输出Fell off,其余情况是:蚂蚁位置+朝向

突发奇想,想做下这题,学习了 lrj 写法。

他的before 和 after 处理,使得蚂蚁在杆子上依次从左到右处理,而且这样做的好处是,不需要对当前的蚂蚁T秒后的位置与已经处理的蚂蚁作比较(检查是否有Turning 情况),大大节省了时间,否则有可能是10000 × 10000 呢~~~order 数组则记下原来最开始时蚂蚁的编号,是为了输出按回原输入啦。

好简洁,好好向他学习^_^

 #include <iostream>
#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]; int order[maxn];
char dirname[][] = {"L", "Turning", "R"}; int main()
{
int N, L, T, n;
while (scanf("%d", &N) != EOF)
{
for (int cas = ; cas <= N; cas++)
{
scanf("%d%d%d", &L, &T, &n);
int dir;
char pos;
for (int i = ; i < n; i++)
{
cin >> dir >> pos;
int d = (pos == 'R' ? : -);
before[i] = (Ant){i, dir, d};
after[i] = (Ant){, dir+d*T, d};
}
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 = ; // 碰撞
}
printf("Case #%d:\n", cas);
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+]); // +1是因为数组下标没有-1
}
puts("");
}
}
return ;
}

uva 10881 Piotr's Ants 解题报告的更多相关文章

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

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

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

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

  8. Uva 10881 Piotr’s Ants 蚂蚁

    一根长度为 L 厘米的木棍上有 n 只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为 1 厘米/秒.当两只蚂蚁相撞时,二者同时调头(掉头用的时间忽略不计).给出每只蚂蚁的初始位置和朝向,计算 T 秒之后 ...

  9. uva 10881 - Piotr's Ants

    这个题的突破点就在于蚂蚁不能够穿过对方,故相对位置不变: 另外,又可以把蚂蚁看成运动方向不变: 代码: #include<cstdio> #include<algorithm> ...

随机推荐

  1. RabbitMQ 核心概念

    目录 RabbitMQ 特点 AMQP 协议 RabbitMQ 消息传递机制 Message Exchange 1. 简介 2. 类型 3. 属性 RabbitMQ 特点 RabbitMQ 相较于其他 ...

  2. Linux环境下编译JDK

    环境准备 操作系统,ubuntu-14.04.6-desktop-amd64.iso,下载地址:http://59.80.44.100/releases.ubuntu.com/14.04/ubuntu ...

  3. screen状态变Attached连接会话失败

    使用xshell远程登录主机,使用screen命令启动程序运行至后台,意外发现screen session的状态为Attached,使用命令screen -r <session-id>,提 ...

  4. mysql存储过程命令行批量插入N条数据命令

    原文:http://blog.csdn.net/tomcat_2014/article/details/53377924 delimiter $$ create procedure myproc () ...

  5. xcode创建一个工程的多个taget,便于测试和发布多个版本

    背景:很多时候,我们需要在一个工程中创立多个target,也就是说我们希望同一份代码可以创建两个应用,放到模拟器或者真机上,或者是,我们平时有N多人合作开发,当测试的时候,在A这里装了一遍测A写的那块 ...

  6. php命令行查看扩展信息

    通常,在php的开发过程中,我们会使用到第三方扩展,这时候,我们对于php扩展的信息的查看就显得尤为重要了.一般情况下,我们查看到扩展信息,都是直接通过 cat *.ini  文件来进行,这样子的效率 ...

  7. linux查看 cpu及内存和硬盘使用情况的命令top

    使用时输入 top,退出时输入q http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316399.html 简介 top命令是Linux下常用的 ...

  8. libevent和libev的区别对比

    参考了这篇文章: http://www.cnblogs.com/Lifehacker/p/whats_the_difference_between_libevent_and_libev_chinese ...

  9. SGPIO

    http://en.wikipedia.org/wiki/SGPIO SGPIO From Wikipedia, the free encyclopedia   Serial General Purp ...

  10. 多媒体开发之---h.264 SPS PPS解析源代码,C实现一以及nal分析器

    http://blog.csdn.net/mantis_1984/article/details/9465909 http://blog.csdn.net/arau_sh/article/detail ...