UVa 10881 Piotr's Ants (等价变换)
题意:一个长度为L的木棍上有n个蚂蚁,每只蚂蚁要么向左,要么向右,速度为1,当两只蚂蚁相撞时,
它们同时掉头。给定每只蚂蚁初始位置和朝向,问T秒后,每只蚂蚁的状态。
析:刚看到这个题时,一点思路也没有,怎么做啊,难道又要模拟么,一想,模拟。。。天呐,好麻烦!
最终还是看了一下题解。真是很巧妙哪。
首先是当两个蚂蚁相撞时,转向和不转向是看不出来的。也就是说掉头等价于对穿而过。也就是说,
如果把蚂蚁看成是没有区别的小点,那么只要独立算每只蚂蚁的位置即可。虽然是这么说,但是,
对每只蚂蚁却不是这样,但是我们只要搞清楚了谁是谁了,就能搞定。
最重要的一点是,每只蚂蚁的相对顺序是不会变的,这才是核心。因此把所有目标位置从小到大排序,
刚从左到右的每个位置都是和初始状态下一样的。由于输入顺序不一定是按从左到右,所以需要预处理一下。
代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std;
typedef long long LL;
const int maxn = 10000 + 10;
struct node{
int id, p, d;
node(int i = 0, int pp = 0, int dd = 0) : id(i), p(pp), d(dd) { }
bool operator < (const node &q) const {
return p < q.p;
}
};
node a[maxn], b[maxn];
int o[maxn]; int main(){
int t, T, l, n, cases = 0; cin >> T;
while(T--){
scanf("%d %d %d", &l, &t, &n);
for(int i = 0; i < n; ++i){
char ch;
scanf("%d %c", &a[i].p, &ch);
a[i].id = i;
a[i].d = (ch == 'L' ? -1 : 1);//-1表示向左,1表示向右
b[i] = node(0, a[i].p + t*a[i].d, a[i].d); //id是未知的
} sort(a, a+n);//按照从左到右的顺序排列
for(int i = 0; i < n; ++i)
o[a[i].id] = i;//确定顺序数组
sort(b, b+n);//计算最后状态 printf("Case #%d:\n", ++cases);
for(int i = 0; i < n; ++i){
int d = o[i];//对应顺序
if(b[d].p < 0 || b[d].p > l) printf("Fell off\n");
else if(b[d].p == b[d+1].p || b[d].p == b[d-1].p) printf("%d Turning\n", b[d].p);
else printf("%d %c\n", b[d].p, b[d].d == 1 ? 'R' : 'L');
}
printf("\n");
}
return 0;
}
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 [题目描述 ...
- 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 ...
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...
- 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 ...
- [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&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 蚂蚁
一根长度为 L 厘米的木棍上有 n 只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为 1 厘米/秒.当两只蚂蚁相撞时,二者同时调头(掉头用的时间忽略不计).给出每只蚂蚁的初始位置和朝向,计算 T 秒之后 ...
随机推荐
- 常用html设置:
省略 居中 1. 省略 ellipsis: text-overflow:ellipsis: 要求容器必须是固定的,要不然无法做省略. table的省略 table{ table_layout:fixe ...
- 在eclipse中修改生成的注释类、方法的作者、时间
1.打开eclipse:windos-->preferences-->Java-->Code Style -->Code Templates-->Types-->点 ...
- Null value was assigned to a property of primitive type setter of"原因及解决方法
在action请求数据的过程中报出"Null value was assigned to a property of primitive type setter of"错误,搜索之 ...
- mark_2017_2_27
工作总结web_acl 535 git clone “ssh://git@outergit.yonyou.com:49622/esn_web/web_acl.git" 600 git bra ...
- sysdig
centos 7 安装 https://sysdig.com/opensource/sysdig/install/ 1) Trust the Draios GPG key, configure the ...
- Process ProcessThread Thread
Process ProcessThread: Process and ProcessThread objects have a ProcessorAffinity property of IntPtr ...
- pidgin的未认证解决办法
安全验证打开还是无法登陆,并且手机无法验证. 解决:开启手机二次验证,给pidgin设置专门的登陆账户密码,即可解决.
- centos 网卡聚合及Cisco交换机链路聚合
一.配置环境 centos 系统.网卡1口和2口做链路聚合. 交换机网口 6口和7口. 二.服务器操作步骤 centos 6 1.创建一个channel bonding interface #v ...
- SSH框架整合过程总结
---------------------siwuxie095 SSH 框架整合过程总结 (一)导入相关 jar 包(共 ...
- PAT L2-010 排座位(floyd)
布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位.无论如何,总不能把两个死对头排到同一张宴会桌旁!这个艰巨任务现在就交给你,对任何一对客人,请编写程序告诉主人他们是否能被安排同席. 输入格式: ...