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

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

然后调换它们的状态(感觉像是障眼法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. ios的xxxAppDelegate.h分析

    #import "BIDAppDelegate.h" #import "BIDViewController.h" @implementation BIDAppD ...

  2. spring 提供的属性值拷贝工具类

    当需要把一个原生的类中属性拷贝到扩展类中时,使用以下类很方便:

  3. iOS单选和全选

    在日常开发中单选.多选.全选经常遇到,所以写一个demo放上来供大家参考, 先看效果图: Demo地址:https://github.com/domanc/SingleAndAllSelect.git

  4. JLRoute 使用

    http://zhoulingyu.com/2016/01/03/iOS%E2%80%94%E2%80%94Scheme%E6%98%AF%E4%BB%80%E4%B9%88%E3%80%81%E6% ...

  5. 制作initramfs/initrd镜像

    Linux kernel在自身初始化完成之后,需要能够找到并运行第一个用户程序(这个程序通常叫做"init"程序).用户程序存在于文件系统之中,因此,内核必须找到并挂载一个文件系统 ...

  6. How to Get the Length of File in C

    How to get length of file in C //=== int fileLen(FILE *fp) { int nRet = -1; int nPosBak; nPosBak = f ...

  7. python用sqlite3模块操作sqlite数据库-乾颐堂

    SQLite是一个包含在C库中的轻量级数据库.它并不需要独立的维护进程,并且允许使用非标准变体(nonstandard variant)的SQL查询语句来访问数据库. 一些应用可是使用SQLite保存 ...

  8. SQL 零碎点

    1,插入数据后,返回主键ID值: INSERT INTO tablename (name) VALUES (@name);SELECT @@Identity; 使用 SELECT @@Identity ...

  9. LWIP协议栈2-

    ->->->

  10. matrix derivatives

    来源:cs229 stanford Machine Learning Notes