题目链接: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. CentOS如何查看端口是被哪个应用/进程占用

    有时启动应用时会发现端口已经被占用,或者是感觉有些端口自己没有使用却发现是打开的.这时我们希望知道是哪个应用/进程在使用该端口. CentOS下可以用netstat或者lsof查看,Windows下也 ...

  2. Activity singleInstance启动模式

    全局单例模式 如果 是新建Activity, 则新建一个Task, 然后将ActivityRecord单独放在其中 如果已经存在这个Activity, 则直接将这个Activity转到前台

  3. Android 简单的FC

    直接贴log 01-02 08:17:56.589 I/ActivityManager( 312): Start proc com.android.providers.calendar for con ...

  4. css格式化排版

    1,文字排版--字体 语法: body{font-family:"Microsoft Yahei";} 这里注意不要设置不常用的字体,因为如果用户本地电脑上如果没有安装你设置的字体 ...

  5. python库tkinter、pygame中几点需要注意的问题

    恍然之间已经16年快四月份了,已经好久都没有写过东西了.. 最近在用python做一些小的游戏,在网上找了一些Python库,Python中游戏编程最常用的还是pygame了,其次是Tkinter p ...

  6. Laravel之路——事务

    准备: 表必须是InnoDB引擎 DB::beginTransaction(); try{ $name = 'abc'; $result1 = Test::create(['name'=>$na ...

  7. Android Learning:数据存储方案归纳与总结

    前言 最近在学习<第一行android代码>和<疯狂android讲义>,我的感触是Android应用的本质其实就是数据的处理,包括数据的接收,存储,处理以及显示,我想针对这几 ...

  8. Struts2技术内幕-----第七章

    1)基于人机交互的请求--响应模式主要由哪三大要素构成?     ①沟通协议-----人和机器都能够明白的数据通信格式     ②请求内容-----人通过某种机制向机器发起的数据请求     ③响应内 ...

  9. commons-lang阅读(一)

    基于commons-lang-2.4.jar org.apache.commons.lang.time.DateFormatUtils类 package org.apache.commons.lang ...

  10. iOS:使用导航栏

    要求使用ARC // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. ...