题目链接:10881 - Piotr's Ants

题目大意:在一个长为L的木棒上有n只蚂蚁,给出蚂蚁的初始位置以及方向,问说移动T秒后各个蚂蚁的位置以及状态,如果两只蚂蚁在移动的过程中相撞,则会同时掉头。

解题思路:问题只要解决说两只蚂蚁相撞的情况就差不多了,其实从整体上来看(不考虑蚂蚁的编号),“相撞”和对穿而过“是一样的,只不过移动到那个位置的蚂蚁并不是先前的那只。所以说只要记录下每只蚂蚁的顺序,它是不会因为移动而跳到另外一只的前面。

#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; const int N = 10005;
const char dirName[][10] = { "L", "Turning", "R" }; struct Ant {
int id;
int p;
int dir;
bool operator < (const Ant& c) const {
return p < c.p;
}
}before[N], after[N];
int L, T, n, order[N]; void input() {
scanf("%d%d%d", &L, &T, &n); int d;
char ch;
for (int i = 0; i < n; i++) {
scanf("%d %c", &before[i].p, &ch);
before[i].id = i;
d = after[i].dir = before[i].dir = (ch == 'L')? -1: 1;
after[i].p = before[i].p + T * d;
}
} void solve() { memset(order, 0, sizeof(order));
sort(before, before + n);
for (int i = 0; i < n; i++)
order[before[i].id] = i; sort(after, after + n);
for (int i = 1; i < n; i++)
if (after[i - 1].p == after[i].p) after[i - 1].dir = after[i].dir = 0;
} void output() {
for (int i = 0; i < n; i++) {
int a = order[i];
if (after[a].p < 0 || after[a].p > L) printf("Fell off\n");
else printf("%d %s\n", after[a].p, dirName[after[a].dir + 1]);
}
printf("\n");
} int main () {
int cas;
scanf("%d", &cas);
for (int i = 1; i <= cas; i++) {
input(); solve(); printf("Case #%d:\n", i);
output();
}
return 0;
}

10881 - Piotr's Ants(排序)的更多相关文章

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

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

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

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

  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. 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. 10881 - Piotr's Ants

    Problem D Piotr's Ants Time Limit: 2 seconds "One thing is for certain: there is no stopping th ...

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

  8. Uva 10881 Piotr’s Ants 蚂蚁

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

  9. UVa 10881 Piotr's Ants (等价变换)

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

随机推荐

  1. root 密码丢失后的重新设置

    /usr/local/mysql/bin/mysqld_safe --skip-grant-tables & mysql> use mysql; mysql> update use ...

  2. Java学习----Math函数

    public class TestMath { public static void main(String[] args) { System.out.println(Math.E); System. ...

  3. 能用存储过程的DBHelper类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. C#一个字符串的加密与解密

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...

  5. iOS+Swift: 使用MessageUI.framework发送短信

    在iOS中, 可以使用MessageUI.framework框架发送短信, 步骤如下: 代码下载http://git.oschina.net/yao_yu/swift_cnblogs_samples/ ...

  6. 2016022602 - redis安装和启动

    redis安装 我使用的是ubuntu15.1,打开终端,输入命令:sudo apt-get install redis-server 将会在本机安装上redis. 启动redis 启动redis命令 ...

  7. php pdo和mysqli对比选择

    1)总的比较   PDO MySQLi 数据库支持 12种不同的数据库支持 支持MySQL API OOP OOP + 过程 Connection Easy Easy 命名参数 支持 不支持 对象映射 ...

  8. 初识Vim

    在Windows系统安装Vim后桌面上会添加gVim.gVim Easy.gVim Read-only 三个快捷方式. gVim 指向主程序,gVim Easy.gVim Read-only 也是,但 ...

  9. autoconf automake libtool

    这是一个 autoconf / automake 的 "Hello World"gztt.ll@gmail.com 主要步骤是- 准备工程目录结构和程序- autoscan 生成 ...

  10. [Quote]Creating basic Excel workbook with Open XML

    Creating basic Excel workbook with Open XML [Quote from]http://www.codeproject.com/Articles/371203/C ...