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 秒之后 ... 
随机推荐
- mysql插入json数据
			data_dict = {"a":1, "b":2} data_json = json.dumps(data_dict) data_escape = MySQ ... 
- struts2 模拟令牌机制防止表单重复提交
			web.xml: <?xml version="1.0" encoding="UTF-8"?><web-app version="3 ... 
- 吴裕雄 python神经网络 水果图片识别(1)
			import osimport numpy as npimport matplotlib.pyplot as pltfrom skimage import color,data,transform,i ... 
- 吴裕雄 实战PYTHON编程(10)
			import cv2 cv2.namedWindow("frame")cap = cv2.VideoCapture(0)while(cap.isOpened()): ret, im ... 
- Appium的inspector使用
			使用inspectot可以对元素进行定位 1.设置appium的Android Settings,点击左上角的安卓图标进入安卓设置,注意设置时不要开启appium 说明: a)Application是 ... 
- jquery 不选择第一个
			参考 https://zhidao.baidu.com/question/174343639.html th:not(':first') 
- web项目生成web.xml的两种方式
			做了很多的项目,今天着手写个小demo发现做web项目的时候还需要从别的地方去拷贝,那么如果没有地方可以拷贝,要怎么办呢?下边介绍三种方式生成web.xml文件. 一.maven项目情况:(STS版) ... 
- svn: authentication cancelled
			从svn 下程序时用户名和密码输入正确后报如图错误! 控制台输出: svn: authentication cancelled svn: authentication cancelled ... 
- 97. Interleaving String (String; DP)
			Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ... 
- phpStudy2——PHP脚本访问MySql数据库
			前言: 前边介绍了php脚本获取并打印输出html提交的数据,本文将介绍php脚本访问查询MySql数据库. 示例代码: <style> td{width:100px;}/*我的存在只为证 ... 
