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

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

然后调换它们的状态(感觉像是障眼法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. apply-register-acl 参数允许FreeSWITCH分机注册/拨打不验证密码

    今天调试 发现 注册的分机 的 `Auth-User` 居然是 `unknown` !!! 怎么回事? 仔细对比检查 发现, internal profile 指定了 `apply-register- ...

  2. 将中国标准时间转成yyyy-MM-dd

    public static void main(String[] args) throws ParseException { String s = "Tue Jul 12 00:00:00 ...

  3. ios crash log

    1.IOS策略 1.1 低内存闪退 前面提到大多数crash日志都包含着执行线程的栈调用信息,但是低内存闪退日志除外,这里就先看看低内存闪退日志是什么样的.我们使用Xcode 5和iOS 7的设备模拟 ...

  4. Python实现常见算法[1]——冒泡排序

    #!/usr/bin/python def BUBBLE_SORT(L, x, y): j = y while j>x: i = x while i<j: if L[i] > L[i ...

  5. Windows 安装 Maven 及 Eclipse 安装Maven插件

    环境说明: window 8.1 64bit Eclipse Version: Luna Release (4.4.0) Maven 3.0.5 Windows Maven 安装: 1.确保安装了JD ...

  6. Ubuntu设置屏幕分辨率

    Ubuntu设置屏幕分辨率 原创 2016年10月14日 13:01:24 14900 在虚拟机装好Ubuntu,进入系统分辨率是800*600,打开显示界面设置下分辨率,设置完怎么也选不上应用,于是 ...

  7. CentOS 6.5 下安装 Sun JDK 1.7

    下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 得到 jdk-7u51-l ...

  8. C++ STL 全排列函数

    C++  全排列函数...一听名字就在<algorithm>中... 首先第一个说的是next_permutation: #include <algorithm> bool n ...

  9. Golang 之 key-value LevelDB

    时常会在应用中用到数据库功能,象 Key-Value 性质的.直接搬个 Redis,Mysql嫌大,好在有 LevelDB,直接编进应用中. 有关什么是 LevelDB 以及 LevelDB 的特性, ...

  10. CentOS7 安装Maven3

    下载安装文件 cd /root wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache- ...