Uva---10881 Piotr's Ants(蚂蚁)
Problem D
Piotr's Ants
Time Limit: 2 seconds
"One thing is for certain: there is no stopping them; the ants will soon be here. And I, for one, welcome our new insect overlords." |
Kent Brockman
Piotr likes playing with ants. He has n of them on a horizontal pole L 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 T seconds from now.
Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containing 3 integers: L , T and n (0 <= n <= 10000). The next n lines give the locations of the n ants (measured in cm from the left end of the pole) and the direction they are facing (L or R).
Output
For each test case, output one line containing "Case #x:" followed by n lines describing the locations and directions of the n 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 before Tseconds, print "Fell off" for that ant. Print an empty line after each test case.
Sample Input | Sample Output |
|
|
Problemsetter: Igor Naverniouk
Alternate solutions: Frank Pok Man Chu and Yury Kholondyrev
代码:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<algorithm>
- #include<vector>
- using namespace std;
- const int maxn=;
- struct ants
- {
- int id ; //输入序号
- int pos ; //在小木棍上的顺序
- int status ; //状态
- bool operator <( const ants an) const
- {
- return pos<an.pos;
- }
- }begin[maxn],end[maxn];
- int order[maxn];
- int main()
- {
- int test;
- int T,L,n;
- char str;
- int a;
- scanf("%d",&test);
- for(int k=;k<=test;k++)
- {
- printf("Case #%d:\n",k);
- scanf("%d%d%d",&L,&T,&n);
- for(int j=;j<n;j++)
- {
- scanf("%d %c",&a,&str);
- int value=(str=='L')?-:;
- begin[k]=(ants){j,a,value};
- end[k]=(ants){,a+T*value,value};
- }
- sort(begin,begin+n);
- for(int i=;i<n;i++)
- order[begin[i].id]=i;
- sort(end,end+n);
- for(int i=;i<n-;i++)
- if(end[i].pos==end[i+].pos)
- end[i].status=end[i+].status=;
- char chastr[][]={"L","Turning","R"};
- for(int i=;i<n;i++){
- int a=order[i];
- if(end[a].pos<||end[a].pos>L)
- printf("Fell off\n");
- else
- printf("%d %s\n",end[a].pos,chastr[end[a].status+]);
- }
- printf("\n");
- }
- return ;
- }
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 [题目描述 ...
- [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 蚂蚁
一根长度为 L 厘米的木棍上有 n 只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为 1 厘米/秒.当两只蚂蚁相撞时,二者同时调头(掉头用的时间忽略不计).给出每只蚂蚁的初始位置和朝向,计算 T 秒之后 ...
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...
- 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 ...
- 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& ...
- 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> ...
随机推荐
- @JoinColumn
@OneToOne注释只能确定实体与实体的关系是一对一的关系,不能指定数据库表中的保存的关联字段.所以此时要结合@JoinColumn标记来指定保存实体关系的配置. @JoinColumn与本书上一章 ...
- 虚拟机guest为windows7的环境下安装破解版simplify3d_3.0.2
情形: 1.主机(host):ubuntu 2.虚拟机里安装的操作系统版本(guest):windows 7专业版 3.simplify3d破解版版本:3.0.2(破解需要的工具均在下文的百度云地址里 ...
- JMS【一】--JMS基本概念
摘要:The Java Message Service (JMS) API is a messaging standard that allows application components bas ...
- css 集锦。
可以是 链接的下划线 去掉. a {text-decoration: none} position:absolute 绝对定位 position:relative 相对定位 ie 图片失真 -ms ...
- php笔记[2]
strlen()函数获得字符窜的长度 读取文件:fgets(),fgetss()和fgetcsv() 读取整个文件:readfile(),fpassthru()和file() 读取一个字符:fgetc ...
- iOS - OC 异常处理
1.@try 语句 @try { // Code that can potentially throw an exception 可能会抛出异常的代码块 statement . . . } @catc ...
- 从网页(WEB)登录SAP
以下这篇文章写得很详细,照着做就可以了: http://www.doc88.com/p-293361232332.html 设好后, 默认的端口是80$$, 其中$$是安装SAP时的instanc ...
- mysql概要(七)表字段管理,字段的增删改
1.添加列 放在某列之后 放第一列: 2.修改(声明和名字),删除列 2.1修改声明 2.2删除列
- 转 git使用命令, 特别:git checkout -b a 与 git branch a区别
创建分支: $ git branch mybranch 切换分支: $ git checkout mybranch 创建并切换分支: $ git checkout -b mybranch 更新mast ...
- ajax实现无刷新上传附件并且显示进度条的实例
首先:得把php.ini中的post_max_size和upload_max_filesize改成200M或更大(进度条好看效果,默认是2M) html和js代码: <!DOCTYPE html ...