题目传送门

 /*
题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态
思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了
关键2:蚂蚁的相对位置不变 关键3:order数组记录顺序
*/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std; const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f;
const char dir_name[][] = {"L", "Turning", "R"};
struct Ant
{
int pos, dir, id;
bool operator < (const Ant &a) const
{
return pos < a.pos;
}
}pre[MAXN], now[MAXN];
int order[MAXN]; int main(void) //UVA 10881 Piotr's Ants
{
// freopen ("UVA_10881.in", "r", stdin); int T; int cas = ; int len, t, n;
scanf ("%d", &T);
while (T--)
{
scanf ("%d%d%d", &len, &t, &n);
char ch;
for (int i=; i<=n; ++i)
{
int p, d; char ch;
scanf ("%d %c", &p, &ch);
d = ((ch=='L') ? - : );
pre[i] = (Ant) {p, d, i};
now[i] = (Ant) {p+t*d, d, };
} sort (pre+, pre++n); //计算相对位置
for (int i=; i<=n; ++i) order[pre[i].id] = i; //输入(输出)的顺序 sort (now+, now++n);
for (int i=; i<n; ++i)
{
if (now[i].pos == now[i+].pos)
now[i].dir = now[i+].dir = ;
} printf ("Case #%d:\n", ++cas);
for (int i=; i<=n; ++i)
{
int x = order[i];
if (now[x].pos < || now[x].pos > len) puts ("Fell off");
else
{
printf ("%d %s\n", now[x].pos, dir_name[now[x].dir+]);
}
} puts ("");
} return ;
} /*
Case #1:
2 Turning
6 R
2 Turning
Fell off Case #2:
3 L
6 R
10 R
*/

思维题 UVA 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. cogs 1456. [UVa 10881,Piotr's Ants]蚂蚁

    1456. [UVa 10881,Piotr's Ants]蚂蚁 ★   输入文件:Ants.in   输出文件:Ants.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述 ...

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

  5. UVA 10881 - Piotr's Ants【模拟+思维】

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  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&category=20&pa ...

  8. uva 10881 - Piotr's Ants

    这个题的突破点就在于蚂蚁不能够穿过对方,故相对位置不变: 另外,又可以把蚂蚁看成运动方向不变: 代码: #include<cstdio> #include<algorithm> ...

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

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

随机推荐

  1. codevs3728 联合权值

    题目描述 Description 输入描述 Input Description 输出描述 Output Description 样例输入 Sample Input 样例输出 Sample Output ...

  2. DELPHI IDFTP

    FTP是一个标准协议,它是在计算机和网络之间交换文件的最简单的方法. FTP也是应用TCP/IP协议的应用协议标准.FTP通常于将作者的文件上传至服务器,或从服务器上下传文件的一种普遍的使用方式作为用 ...

  3. poj——3687 Labeling Balls

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14835   Accepted: 4346 D ...

  4. codevs——1006 等差数列

    1006 等差数列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 给定n(1<=n< ...

  5. 选择器的使用(empty选择器)

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  6. fetch各种报跨域错误,数据无法获取的解决方案

    1.介绍 fetch 提供了一个获取资源的接口 (包括跨域). fetch 的核心主要包括:Request , Response , Header , Body 利用了请求的异步特性 --- 它是基于 ...

  7. hadoop2.2集群搭建问题只能启动一个datanode问题

    按照教程http://cn.soulmachine.me/blog/20140205/搭建总是出现如下问题: 2014-04-13 23:53:45,450 INFO org.apache.hadoo ...

  8. 智能眼镜技术科普:VR、AR、MR的区别

    前段时间, 获得谷歌5亿美元融资的技术公司Magic Leap在WSJD展会中放出了一段实录视频,引起不小骚动.如今,也有媒体称他们为MR公司,那么VR.AR.MR之间到底有什么区别呢. VR.AR. ...

  9. Swift开发iOS项目实战视频教程(一)---iOS真简单

    本课主要介绍iOS项目的创建.第一个iOS项目的开发.UILabel.UIButton的使用. 假设你看完此视频还认为iOS非常难,请你来找我! 本教程摒弃枯燥的语法和知识解说,全是有趣有料的项目实战 ...

  10. NYOJ 55 懒省事的小明(哈弗曼树)

    懒省事的小明 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述       小明非常想吃果子,正好果园果子熟了. 在果园里,小明已经将全部的果子打了下来,并且按果子的不 ...