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> ...
随机推荐
- java递归查询方法
一.需求 项目里要让用户能够设置所选择教材的章课节,以针对章课节提供相应的题目供用户做题. 设计:用户设置了教材后,首次登录,进行章节设置时.默认为用户选择第一章.第一课.第一节. 思路:用户访问页面 ...
- [设计模式] 21 策略模式 Strategy
在GOF的<设计模式:可复用面向对象软件的基础>一书中对策略模式是这样说的:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.该模式使得算法可独立于使用它的客户而变化. 策略模 ...
- Bad configuration option localCommand
command-line: line 0: Bad configuration option: PermitLocalCommand 2011-12-08 14:04:54 标签:Bad confi ...
- Appboy 基于 MongoDB 的数据密集型实践
摘要:Appboy 正在过手机等新兴渠道尝试一种新的方法,让机构可以与顾客建立更好的关系,可以说是市场自动化产业的一个前沿探索者.在移动端探索上,该公司已经取得了一定的成功,知名产品有 iHeartM ...
- POJ 1804
/* *由此题发现规律 就是冒泡排序的交换次数等于这个数列的逆序对 的对数! */ #include<iostream> #include<stdio.h> #include& ...
- 李洪强iOS学习交流群-iOS大神群
iOS学习大神群-群号:483959373
- 【PHP高效搜索专题(1)】sphinx&Coreseek的介绍与安装
我们已经知道mysql中带有"%keyword%"条件的sql是不走索引的,而不走索引的sql在大数据量+大并发量的时候,不仅效率极慢还很有可能让数据库崩溃.那我们如何通过某些关键 ...
- SpringMVC学习总结(三)——Controller接口详解(1)
4.12.ParameterizableViewController 参数化视图控制器,不进行功能处理(即静态视图),根据参数的逻辑视图名直接选择需要展示的视图. <bean name=&quo ...
- Filter(过滤器)常见应用
孤傲苍狼 只为成功找方法,不为失败找借口! javaweb学习总结(四十六)——Filter(过滤器)常见应用 一.统一全站字符编码 通过配置参数charset指明使用何种字符编码,以处理Html F ...
- json的数据格式(仔细查看)
1.json对象就是jsonObject,jsonobject里可以放入很多键值对,并以逗号为分隔符. jsonObject里还可以嵌套JsonObject对象,或者数组信息作为value,数组作为k ...