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 秒之后 ...
随机推荐
- tensorflow Process finished with exit code 137 (interrupted by signal 9: SIGKILL) 错误
Process finished with exit code 137 (interrupted by signal 9: SIGKILL) 在使用tensorflow自带的数据集做手写数字识别的时候 ...
- 修改rabbitmq Web UI 监控页面的端口
在前几天工作中遇到一个问题,部署服务器,需要用rabbitmq自带的一个web UI监控组件,但是15672的端口没有对外映射.尝试了几种办法.开始修改rabbitmq.config,rabbitmq ...
- 【342】Linear Regression by Python
Reference: 用scikit-learn和pandas学习线性回归 首先获取数据存储在 pandas.DataFrame 中,获取途径(CSV 文件.Numpy 创建) 将数据分成 X 和 y ...
- python 形参
def fun(x="dx",y="dy"): print "fun------" print x print y fun()fun(&qu ...
- jquery parents用法
之前一直用find 现在用parents var w = $("div"); w = $("div").parents('.class'); //在Parent ...
- keynote
[keynote] 1.如何保证文档加载完才运行代码? 2.元素选择器. 3.属性选择器. 4.更新css. 5.更复杂的例子. 6.常用事件. 7.hide & slow 8.您可以使用 t ...
- np.hsplit()
numpy.hsplit numpy.hsplit(ary, indices_or_sections)[source] Split an array into multiple sub-arrays ...
- 4-计算九位数以内各个位数字和为s的种类
/*假设我们当前要求的是100以内各位数之和和等于s的数有多少个,可以想到就是10以内各位数之和等于s的数的个数再加上10到100内的各位数之和等于s的个数.令dp[i][j]就代表10的j次方内各位 ...
- MD5加密获得文件的MD5码
哈希函数将任意长度的二进制字符串映射为固定长度的小型二进制字符串.加密哈希函数有这样一个属性:在计算不大可能找到散列为相同的值的两个不同的输入:也就是说,两组数据的哈希值仅在对应的数据也匹配时才会匹配 ...
- 子类覆写的变量被private隐藏,强制转换方式通过子类访问父类的被覆写变量:
import static java.lang.System.*; public class SuperParent{ public static void main(String[] args){ ...