题目链接:https://vjudge.net/problem/UVA-10881

其实这道题的关键只有一句话:

   当两个蚂蚁因碰撞而掉头的时候,我们完全可以认为是两个点对穿而过。

这时候我们的主要任务就是弄清楚“谁是谁”。

然而很明显每只蚂蚁的相对顺序是不变的,所以我们要记录一个$order$数组。

预处理出它的初始状态和order,同时算出走之后的状态。

注意程序中的有些地方处理的很巧妙。

AC代码:

 #include<cstdio>
#include<iostream>
#include<algorithm> using namespace std; const int maxn=;
struct node{
int id;
int pos;
int dir;
bool operator < (const node& a) const{
return pos<a.pos;
}
} before[maxn],after[maxn];
const char dirname[][]={"L","Turning","R"};
int order[maxn]; int main(){
int kase;
scanf("%d",&kase);
for(int k=;k<=kase;k++){
int L,T,n;
printf("Case #%d:\n",k);
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]=(node){i,p,d};
after[i]=(node){,p+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].pos==after[i+].pos) after[i].dir=after[i+].dir=;
for(int i=;i<n;i++){
int a=order[i];
if(after[a].pos<||after[a].pos>L) printf("Fell off\n");
else printf("%d %s\n",after[a].pos,dirname[after[a].dir+]);
}
printf("\n");
}
return ;
}

AC代码

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 (等价变换)

    题意:一个长度为L的木棍上有n个蚂蚁,每只蚂蚁要么向左,要么向右,速度为1,当两只蚂蚁相撞时, 它们同时掉头.给定每只蚂蚁初始位置和朝向,问T秒后,每只蚂蚁的状态. 析:刚看到这个题时,一点思路也没有 ...

随机推荐

  1. php 获取header信息

    nginx服务器获取header信息: 如:获取token: $_SERVER['HTTP_TOKEN']; 获取自定义的参数采用$_SERVER['参数名'] Apache服务器获取header信息 ...

  2. c语言 printf格式化输出

    #include <iostream> #include<stdio.h> #include <cstring> using namespace std; int ...

  3. 自定义Nginx返回页面

    1.403返回页面 #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log noti ...

  4. 04-Java基础语法【IDEA、方法】

    重要内容记录: 01.IDE介绍 IDE(Integarted Development Environment)是Java集成开发环境,是一种专门用来提高Java开发效率的软件. 免费的IDE:Ecl ...

  5. Ant风格表达式

    1. ?:匹配任意一个字符 *   :匹配0或者任意数量的字符 **  :匹配0或更多的目录

  6. 前端框架-Vue 入门

    一.介绍 1.Vue.js 是什么 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架. Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合. ...

  7. IDEA更换背景颜色与字体

    打开IDEA 选择左上角的File  ->  Setting -> Editor  -> Color Scheme  就可以设置  黑白背景  默认为白色  可以选择  Darcul ...

  8. EasyExcel实现导入excel

    https://blog.csdn.net/rexueqingchun/article/details/91870372 1.pom.xml配置依赖包 <!-- xls格式excel依赖包 -- ...

  9. python接口自动化之fiddler使用(二)

    1.快捷设置,自定义会话框,查看get和post请求 (1)鼠标放在#后面,右键 (2)选择Customize columns (3)选择Miscellaneous (4)选择RequestMetho ...

  10. 比较一下数据结构的链表和linux i2c驱动难度比较