【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁
</pre></center><center style="font-family: Simsun;font-size:14px;"><span style="font-size:32px;"><strong>Problem D</strong></span><span style="font-size:24px;"><strong>Piotr's Ants</strong></span><span style="font-size:18px;">Time Limit: 2 seconds</span></center><p style="font-family: Simsun;font-size:14px;"></p><table border="0"><tbody><tr><td><small><em>"One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one, welcome ournew insect overlords."</em></small></td></tr></tbody></table><p align="right" style="font-family: Simsun;font-size:14px;"><tt>Kent Brockman</tt></p><p class="paragraph" style="font-family: Simsun;font-size:14px;">Piotr likes playing with ants. He has <strong>n</strong> of them on a horizontal pole <strong>L</strong> cm long. Each ant is facing either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it is facing and wants to calculate where the ants will end up <strong>T</strong> seconds from now.</p><p class="paragraph" style="font-family: Simsun;font-size:14px;"><span style="font-size:24px;"><strong>Input</strong></span>The first line of input gives the number of cases, <strong>N</strong>. <strong>N</strong> test cases follow. Each one starts with a line containing 3 integers: <strong>L</strong> , <strong>T</strong> and <strong>n</strong> <nobr>(0 <= <strong>n</strong> <= 10000)</nobr>. The next <strong>n</strong> lines give the locations of the <strong>n</strong> ants (measured in cm from the left end of the pole) and the direction they are facing (L or R).</p><p class="paragraph" style="font-family: Simsun;font-size:14px;"><span style="font-size:24px;"><strong>Output</strong></span>For each test case, output one line containing "Case #<strong>x</strong>:" followed by <strong>n</strong> lines describing the locations and directions of the <strong>n</strong> ants in the same format and order as in the input. If two or more ants are at the same location, print "Turning" instead of "L" or "R" for their direction. If an ant falls off the pole <em>before</em> <strong>T</strong>seconds, print "Fell off" for that ant. Print an empty line after each test case.</p><table width="100%" cellspacing="0" bordercolor="black" border="1" cellpadding="5" style="font-family: Simsun;"><tbody><tr valign="TOP"><td><span style="font-size:24px;"><strong>Sample Input</strong></span></td><td><span style="font-size:24px;"><strong>Sample Output</strong></span></td></tr><tr bgcolor="#D0D0D0" valign="TOP"><td><pre>2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Case #1:
2 Turning
6 R
2 Turning
Fell off Case #2:
3 L
6 R
10 R
一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为1厘米/秒。当两只蚂蚁相撞时,二者同时掉头(掉头时间忽略不计)。给出每只蚂蚁的初始位置和朝向,计算T秒之后每只蚂蚁的位置。
(n<=10000)
思路:
如果不思考的去模拟的话,,显然要在10000的数据面前跪
要发现到二个性质:
1.如果两个蚂蚁相撞可以看做擦肩而过。
2.蚂蚁从左端开始的排列顺序永远不变。
这样就可以根据性质一直接算出所有蚂蚁最终状态(但不知道那个蚂蚁的编号)
再根据性质二推算出每个蚂蚁应该在的位置(即知道蚂蚁是哪只初始状态的)
一道很巧妙的模拟
很可耻的看了题解,浪费的一道好题
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct node
{
int pos;
char direct;
int rank;
}node;
node ant[10100];
int RANK[10100];
int cmp(const void *i,const void *j)
{
node *ii=(node *)i,*jj=(node *)j;
return ii->pos-jj->pos;
}
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int K,KK;
int L,T,n;
while(scanf("%d",&K)!=EOF)
{
KK=K;
while(K--)
{
scanf("%d%d%d",&L,&T,&n);
for(int i=1;i<=n;i++)
{
scanf("%d %c",&ant[i].pos,&ant[i].direct);
ant[i].rank=i;
}
qsort(ant+1,n,sizeof(ant[1]),cmp);
for(int i=1;i<=n;i++)
{
RANK[ant[i].rank]=i;
if(ant[i].direct=='R')
ant[i].pos+=T;
else ant[i].pos-=T;
}
qsort(ant+1,n,sizeof(ant[1]),cmp);
for(int i=1;i<=n;i++)
if(ant[i].pos==ant[i-1].pos) ant[i-1].direct=ant[i].direct='T';
printf("Case #%d:\n",KK-K);
for(int i=1;i<=n;i++)
{
if(ant[RANK[i]].pos>=0&&ant[RANK[i]].pos<=L)
if(ant[RANK[i]].direct=='T')
printf("%d Turning\n",ant[RANK[i]].pos);
else
printf("%d %c\n",ant[RANK[i]].pos,ant[RANK[i]].direct);
else printf("Fell off\n");
}
printf("\n");
}
}
return 0;
}
【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁的更多相关文章
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...
- [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 (思维题)
UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...
- 【UVa 10881】Piotr's Ants
Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...
- 思维题 UVA 10881 Piotr's Ants
题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...
- 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 ...
- 10881 - Piotr's Ants(排序)
题目链接:10881 - Piotr's Ants 题目大意:在一个长为L的木棒上有n只蚂蚁,给出蚂蚁的初始位置以及方向,问说移动T秒后各个蚂蚁的位置以及状态,如果两只蚂蚁在移动的过程中相撞,则会同时 ...
- 10881 - Piotr's Ants
Problem D Piotr's Ants Time Limit: 2 seconds "One thing is for certain: there is no stopping th ...
随机推荐
- Android系统匿名共享内存Ashmem(Anonymous Shared Memory)驱动程序源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6664554 在上一文章Android系统匿名共 ...
- 执行update操作的话,就会报“Connection is read-only. Queries leading to data modification are not allowed”的异常。
我用的是 spring + springmvc + mybatis +mysql. <tx:advice id="txAdvice" transaction-manager= ...
- 面试前的准备---C#知识点回顾----03
经过一天的奔波,喜忧参半,不细表 再回看下标题,C#知识点回顾 再看下内容,数据库3NF 原谅我这个标题党 今天继续回忆 1.HTTP中Post和Get区别 这忒简单了吧,大家是不是感觉到兴奋了,长舒 ...
- ajax参数中出现空格
jquery中发起ajax请求时的参数名中不能有空格.如果是get请求参数的中的空格会变成“+”符而在post请求中看不到这种变化,但无论哪种情况都无法与服务接口的参数就行匹配(此时进行调试也不会触发 ...
- SQL按汉语拼音首字母排序
以常用到的省的数据表(province)为例,其中name字段为省的名称,SQL语句如下: ))) as py ,a.name from province a left outer join ( se ...
- linux 下执行.sh文件总是提示permission denied
linux 下执行.sh文件总是提示permission denied 如果你是root登陆的话(不是的话,切换到root用户,对*.sh赋可执行的权限) chmod 777 *.sh or ch ...
- Entity Framework数据库迁移
1.启用数据迁移: enable-Migrations2.增加一条数据库迁移指令:add-Migrations 必须带上一个版本名称,比如AddUsernamePassword完整的指令:add-Mi ...
- JS 混淆加密
http://www.javascriptobfuscator.com/Javascript-Obfuscator.aspx http://www.javascriptobfuscator.com/d ...
- C++11新特性
c++语言的扩展和修正,不仅包含了核心语言的新机能,而且扩展了c++标准库(STL),并引入了大部分的C++ technical report 1程序库 C++11还包括大量新特性:包括lambda表 ...
- jquery file upload 后台收到的文件名中文乱码, filename中文乱码
在jQuery File Upload.js文件里,在以下这个js中有个成员叫做 _initXHRData, 是一个function, 在这个function的最后部分有一个if-else分支,如下: