Uva 10881 Piotr’s Ants 蚂蚁
一根长度为 L 厘米的木棍上有 n 只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为 1 厘米/秒。当两只蚂蚁相撞时,二者同时调头(掉头用的时间忽略不计)。给出每只蚂蚁的初始位置和朝向,计算 T 秒之后每只蚂蚁的位置。
由于掉头用的时间可以忽略不计,所以可以直接看做两只蚂蚁对穿而过。于是可以很开心的直接把坐标按照方向进行加 / 减的处理。得到某只蚂蚁的最终坐标。
把棍子拉为无限长,然后通过模拟这个过程可以发现,蚂蚁的顺序是绝对的,最左边的蚂蚁绝不可能爬到其他蚂蚁的右边去。所以将最终坐标从小到大排序就能够得到这只蚂蚁最终的位置。当然,如果最终蚂蚁的位置坐标小于 0 或者大于 L 那么就直接判断为Fell off。
此外,从题目所给的数据可以看得出,蚂蚁并不是按照顺序输出的,因此用数组存下输入的顺序就OK了。
P.S : 注意每组数据之间有个空行。
附AC代码:
1: #include <stdio.h>
2: #include <math.h>
3: #include <iostream>
4: #include <cstdarg>
5: #include <algorithm>
6: #include <string.h>
7: #include <stdlib.h>
8: #include <string>
9: #include <list>
10: #include <vector>
11: #include <map>
12: #define LL long long
13: #define M(a) memset(a, 0, sizeof(a))
14: using namespace std;
15:
16: void Clean(int count, ...)
17: {
18: va_list arg_ptr;
19: va_start (arg_ptr, count);
20: for (int i = 0; i < count; i++)
21: M(va_arg(arg_ptr, int*));
22: va_end(arg_ptr);
23: }
24:
25: typedef struct Ant
26: {
27: int id, p, d;
28: bool operator < (const Ant& x) const
29: {
30: return p < x . p;
31: }
32: }Ant;
33: Ant now[10009], next[10009];
34:
35: int tmp[10009];
36:
37: int main()
38: {
39: int T;
40: scanf("%d", &T);
41: for (int cnt = 1; cnt <= T; cnt++)
42: {
43: int l, t, n;
44: int p, d;
45: char c;
46: printf ("Case #%d:\n", cnt);
47: scanf("%d%d%d", &l, &t, &n);
48: for (int i = 0; i < n; i++)
49: {
50: scanf("%d %c", &p, &c);
51: d = ((c == 'L') ? (-1) : (1));
52: now[i] = (Ant) {i, p, d};
53: next[i] = (Ant) {0, p + t * d, d};
54: }
55: sort(now, now + n);
56: for (int i = 0; i < n; i++)
57: tmp[now[i] . id] = i;
58: sort(next, next + n);
59: for (int i = 0; i < n; i++)
60: if (next[i] . p == next[i + 1] . p)
61: next[i] . d = next[i + 1] . d = 0;
62: for (int i = 0; i < n; i++)
63: {
64: int temp = tmp[i];
65: if(next[temp] . p < 0 || next[temp] . p > l)
66: puts("Fell off");
67: else
68: {
69: printf("%d ", next[temp] . p);
70: if (next[temp] . d == 0) puts("Turning");
71: else puts((next[temp] . d == 1) ? ("R") : ("L"));
72: }
73: }
74: puts("");
75: }
76: return 0;
77: }
Uva 10881 Piotr’s Ants 蚂蚁的更多相关文章
- cogs 1456. [UVa 10881,Piotr's Ants]蚂蚁
1456. [UVa 10881,Piotr's Ants]蚂蚁 ★ 输入文件:Ants.in 输出文件:Ants.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述 ...
- [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 ...
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...
- UVA.10881 Piotr's Ants (思维题)
UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...
- 思维题 UVA 10881 Piotr's Ants
题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...
- 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 ...
- UVA 10881 - Piotr's Ants【模拟+思维】
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 10881 Piotr's Ants 解题报告
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&pa ...
- uva 10881 - Piotr's Ants
这个题的突破点就在于蚂蚁不能够穿过对方,故相对位置不变: 另外,又可以把蚂蚁看成运动方向不变: 代码: #include<cstdio> #include<algorithm> ...
随机推荐
- Microsoft SQLServer有四种系统数据库
Microsoft SQLServer有四种系统数据库: 1.master数据库 master数据库记录SQLServer系统的所有系统级别信息.它记录所有的登录帐户和系统配置设置.master数据库 ...
- 在线最优化求解(Online Optimization)之一:预备篇
在线最优化求解(Online Optimization)之一:预备篇 动机与目的 在实际工作中,无论是工程师.项目经理.产品同学都会经常讨论一类话题:“从线上对比的效果来看,某某特征或因素对xx产品的 ...
- Eclipse 中Alt+/快捷键失效的解决办法。
1.Eclipse下进入Windows ->Preperences ->General ->keys2.把word completion的快捷键设置alt+/删掉! 3.把Conte ...
- MySQL 5.7原生JSON格式支持
在MySQL与PostgreSQL的对比中,PG的JSON格式支持优势总是不断被拿来比较.其实早先MariaDB也有对非结构化的数据进行存储的方案,称为dynamic column,但是方案是通过BL ...
- hdu 2196
树形dp 本文出自 http://blog.csdn.net/shuangde800 题目传送门 题意: 给出一棵树,求离每个节点最远的点的距离 思路: 把无根树转化成有根树分析, 对于上面那棵树 ...
- winform Chart控件 获取鼠标处坐标值方法
Chart控件本身功能强大,应用广泛,因此其属性.方法也很多.此处介绍在很多应用中需要查看鼠标位置处坐标值的一些方法 1,调用Chart事件 GetToolTip 利用ToolTipEventArg ...
- JavaSE GUI显示列表 JTable的刷新 重新加载新的数据
JTable在显示所有数据之后,假如需要搜索某个名字,则会获取新的列表数据. 假设datas是JTable的数据,定义为: private Vector<Vector> datas = n ...
- 解决maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e.错误
POM文件报错maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supp ...
- switch… case 语句的用法
switch… case 语句的用法 public class Test7 { public static void main(String[] args) { int i=5; switch(i ...
- Debug过程中的mock (及display窗口的使用)
转载:http://m.blog.csdn.net/blog/u012516903/18004965 在debug的时候,有3个地方可以进行mock测试 测试代码如下: 1.使用display窗口 W ...