题目链接: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. pc110301QWERTYU

    水题一道,SOLVED只是次数的问题.map一下,就是很easy啦. #include<iostream> #include<cstdio> #include<cstri ...

  2. 无线端web开发学习总结

    无线web开发之前要做一些准备工作:一.必需的reset样式库1.其中的重点是盒模型box-sizing:由原来pc端的content-box改为border-box. *, *:before, *: ...

  3. node初步一:HTTP请求

    一. 创建pathtest.js文件 var http= require('http' ); var url= require('url' ); function start (){ function ...

  4. 解决SDK Manager无法更新问题

    因为google被封了,导致Android SDK Manager无法更新,解决方案如下: 1.选择tools->options,跳出Settings页面 2.设置HTTP Proxy代理,设置 ...

  5. Silverlight中在MVVM模式下对DatagridRow选择控件封装

    在项目中,凡是涉及到表格的地方用的最多的控件,自然少不了DataGrid的身影,它明了的展示各种数据让人十分喜欢.现在要实现一个功能,使DataGrid具有全选和项选中的功能,如果在传统后台代码中完成 ...

  6. make xxx Is a directory. Stop.

    转自:make xxx Is a directory. Stop. 的原因 make xxx Is a directory. Stop. 的原因 编译内核时候的一个错误提示 make: ***    ...

  7. 面向对象js瀑布流效果

    index.html <!doctype html><html lang="en"> <head>  <!--网站编码格式,UTF-8 国 ...

  8. IbatisNet开发使用小结

    一.   介绍 平常做企业级应用,需求变化是经常的事,而很多基础代码重复也是很让人头疼的问题.所以很多人会使用一些ORM框架来增强项目的可维护性.可扩展性.IBatis.Net就是一个比较易用的ORM ...

  9. 玩SSH,SFTP

    更改SFTP的本地路径,记得前面要加l哟,应该表示local的意思.如lls,lcd. 证书SSH更安全.就是多服务布置有些烦琐~~

  10. 【UVA10537】The Toll! Revisited (逆推最短路)

    题目: Sample Input1a Z19 a Z5A DD XA bb cc X39 A X-1Sample OutputCase 1:20a-ZCase 2:44A-b-c-X 题意: 有两种节 ...