题目就不再写了,直接开始

【分析】蚂蚁碰撞掉头,其实不用考虑掉头问题,直接让“掉头”等价于“对穿而过”,

然后调换它们的状态(感觉像是障眼法hhh),只要分清楚“谁是谁”。因为“掉头”,所以蚂蚁

的相对顺序是不变的,因此把所有目标位置从小到大排序,则从左到右的每个位置都对应初始

状态的每只蚂蚁的位置。由于原题中每只蚂蚁不一定按照从左到右顺序输入,还需要预处理

计算输入中的第i只蚂蚁的序号order[i] 。

【代码】

//2019.4.21 蚂蚁
#include <iostream>
#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_s("%d", &k);
for (int kase = ; kase <= k; kase++)
{
int L, T, n;
cout << "Case #" << k << ":\n";
scanf_s("%d%d%d", &L, &T, &n);
for (int i = ; i < n; i++)
{
int p, d;
char c;
scanf_s("%d %c", &p, &c);
d = (c == 'L' ? - : );
before[i] = { i,p,d };
after[i] = { ,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)
cout << "Fell off\n";
else
cout << after[a].p << " " << dirName[after[a].d + ] << "\n";
}
cout << "\n";
}
return ;
}

UVA 10881的更多相关文章

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

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

  2. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  3. 思维题 UVA 10881 Piotr's Ants

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

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

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

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

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

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

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

  7. uva 10881 Piotr's Ants 解题报告

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

  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 蚂蚁

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

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

随机推荐

  1. ui-router 留存

    学习 ui-router - 路由控制 022014年01月 参考原文:https://github.com/angular-ui/ui-router/wiki/URL-Routing 在你的应用中大 ...

  2. Alternative PHP Cache ( APC )

    简介: Alternative PHP Cache (APC) 是一个开放自由的PHP opcode 缓存.它的目标是提供一个自由.开放和健全的框架用于缓存和优化 PHP 的中间代码,加快 PHP 执 ...

  3. MySQL数据库篇之索引原理与慢查询优化之一

    主要内容: 一.索引的介绍 二.索引的原理 三.索引的数据结构 四.聚集索引与辅助索引 五.MySQL索引管理 六.测试索引 七.正确使用索引 八.联合索引与覆盖索引 九.查询优化神器--explai ...

  4. Gouraud Shading

    [Gouraud Shading] Gouraud Shading (高洛德着色/高氏着色) 这种着色的效果要好得多,也是在游戏中使用最广泛的一种着色方式.它可对3D模型各顶点的颜色进行平滑.融合处理 ...

  5. 32. Longest Valid Parentheses (Stack; DP)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. Eclipse设置Tab键缩进4个空格的步骤,也就是按一下Tab键输出四个空格

    Eclipse设置Tab键缩进4个空格的步骤,也就是按1下Tab键输出4个空格,步奏如下 1.点击 window->preference-,选择 General->Editors-> ...

  7. 深入理解mysql的隔离级别

    建表插入测试数据A> create table test(id int ,num int) ;Query OK, 0 rows affected (0.53 sec) A> insert ...

  8. 16-多线程爬取糗事百科(python+Tread)

    https://www.cnblogs.com/alamZ/p/7414020.html   课件内容 #_*_ coding: utf-8 _*_ ''' Created on 2018年7月17日 ...

  9. Openssl ciphers命令

    一.简介 ciphers指令是用来展示用于SSL加密算法的工具 二.语法 openssl ciphers [-v] [-ssl2] [-ssl3] [-tls1] [cipherlist] 选项 -v ...

  10. scala开发环境

    1. Intellij IDEA Scala开发环境搭建 Intellij IDEA 15.0.3 默认配置里面没有Scala插件,需要手动安装,在Intellij IDEA 15.0.3 第一次运行 ...