思维题 UVA 10881 Piotr's Ants
- /*
- 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过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的更多相关文章
- UVA.10881 Piotr's Ants (思维题)
UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...
- cogs 1456. [UVa 10881,Piotr's Ants]蚂蚁
1456. [UVa 10881,Piotr's Ants]蚂蚁 ★ 输入文件:Ants.in 输出文件:Ants.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述 ...
- 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& ...
- [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 ...
- 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> ...
- UVa 10881 Piotr's Ants (等价变换)
题意:一个长度为L的木棍上有n个蚂蚁,每只蚂蚁要么向左,要么向右,速度为1,当两只蚂蚁相撞时, 它们同时掉头.给定每只蚂蚁初始位置和朝向,问T秒后,每只蚂蚁的状态. 析:刚看到这个题时,一点思路也没有 ...
随机推荐
- Android TextView内容过长加省略号
在Android TextView中有个内容过长加省略号的属性,即ellipsize,用法如下: 在xml中: android:ellipsize = "end" //省略号在结尾 ...
- chdoj38 K-partite Graph(补图)
题意: 若一个无向图G的节点能够分成k(k>=2)个非空集合,对于每对点,当且仅当他们属于不同的集合,存在一条边(ui,vi)连接他们.那么这个图就是一个完全k分图. 现在给出一个n点,m条边的 ...
- Redis官方文档资源
官方文档: 如果要深入研究时,官方提供的文档是最权威的. 英文: https://redis.io/documentation 中文: http://www.redis.cn/documentatio ...
- JSP中自动刷新
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/auto-refresh.html: 细想一个显示在线比赛分数.股市状态或当前交易额的网页.对于所有这种类 ...
- MongoDB小结13 - find【2个参数】
第一个参数 MongoDB使用find来进行查询,查询呢,就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合. find的第一个参数决定了要返回那些文档,其形式也就一个文档,说明了要执行的 ...
- "Simple Factory" vs "Factory Method" vs "Abstract Factory" vs "Reflect"
ref: http://www.cnblogs.com/zhangchenliang/p/3700820.html 1. "Simple Factory" package torv ...
- dataTables 添加行内操作按钮
在上一篇博客中我们提到了用dataTables 做一个分页表格.今天进一步进阶,做一个行内带操作按钮的表格.效果如图. 记得最基础的实现方式是,我们要在js 中拼接字符串,嵌入一个带按钮的语句.但是现 ...
- bzoj 1266 [AHOI2006] 上学路线 route 题解
转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23989499 [原题] 1266: [AHOI2006]上学路线route Time ...
- HDU-3295-An interesting mobile game(BFS+DFS)
Problem Description XQ,one of the three Sailormoon girls,is usually playing mobile games on the clas ...
- C# 实现WEBSOCKET聊天应用示例
C# 实现WEBSOCKET聊天应用示例 http://blog.163.com/da7_1@126/blog/static/10407267820121016103055506/ 2012-11-1 ...