算法竞赛入门经典-训练指南(10881-Piotr's Ants)
题目大意:
一根长度为L的木棍一堆蚂蚁爬,向左或向右,速度都为1,若两蚂蚁碰撞则同时转头(转身时间忽略不计),问T时间之后每只蚂蚁的位置;
输入:t,(t个样例),每个样例输入 L,T,n,接下来是n行每行两个数据,一个POS(位置),一个dir(方向);
输出:按输入顺序输出每只蚂蚁的最终位置,若处于碰撞状态则输出Turning,掉下去输出”Fell off“;
解题思路:
本题类似于《挑战程序设计》的一道水题(POJ -1852 Ants),思路题;不过本题输入并不一定是按照位置大小进行输入的,因此,需要一个order数组进行预处理,具体看代码及注释;
AC代码:
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; /**************************************************** 样例:
2
10 1 4
1 R
5 R
3 L
10 R ****************************************************/ const int maxn = ;
struct ant
{
int id; //输入顺序;
int pos; //实际位置;
int dir; //朝向:
}before[maxn],after[maxn]; bool cmp(ant a,ant b) //按照位置进行排序
{
return a.pos<b.pos;
} const char dire[][]={"L","Turning","R"}; //数组保存输出结果
int order[maxn]; //order数组的作用是保存各蚂蚁终态的相对位置; int main()
{
int t1;
cin>>t1;
for (int kase=;kase<=t1;kase++)
{
cout<<"Case #"<<kase<<":"<<endl;
int l,t,n;
cin>>l>>t>>n;
for (int i=;i<n;i++)
{
int x;
char c;
cin>>x>>c;
int d=(c=='L'?-:);
before[i]=ant{i,x,d};
after[i]=ant{,x+d*t,d}; //朝向依旧是d,因为必有一只蚂蚁的朝向是原来的d,比如(1,R)->(3,R),只不过不知道是那只蚂蚁;
}
sort(before,before+n,cmp);
for (int i=;i<n;i++)
{
order[before[i].id]=i; //before[i].id 是原来数组的输入顺序,现在before按照位置进行排序,那么此时对应的就是当前位置蚂蚁的编号;
//比如样例中排序后before[1]是位置3的蚂蚁,其顺序为3,那么就是order[3]=2;
//那么样例中的order就是 1 3 2 4;
//之后after数组的输出after[order[1]].pos就是5这个位置的末位置;
}
sort(after,after+n,cmp);
for (int i=;i<n-;i++)
{
if (after[i].pos==after[i+].pos)
{
after[i].dir=after[i+].dir=;
}
}
for (int i=;i<n;i++)
{
int a=order[i];
if (after[a].pos < ||after[a].pos>l) cout<<"Fell off"<<endl;
else
{
cout<<after[a].pos<<" "<<dire[after[a].dir+]<< endl;
}
}
cout<<endl;
}
return ;
}
算法竞赛入门经典-训练指南(10881-Piotr's Ants)的更多相关文章
- 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...
- 算法竞赛入门经典 LA 4329(树状数组)
题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- [刷题]算法竞赛入门经典 3-12/UVa11809
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...
- [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...
- [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...
- [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...
随机推荐
- resolution will not be reattempted until the update interval of repository-group has elapsed or updates are forced
Failed to execute goal on project safetan-web: Could not resolve dependencies for project com.safeta ...
- ZOC7 for Mac连接CentOS7无法输入中文问题
确定是ZOC7的问题 改为 iTerm2 加 ZSH 能够输入中文了 自己配置profile 慢慢所有的终端都用iTerm2 渐渐放弃ZOC7
- python - 添加文件环境变量
#添加 当前文件目录 import sys,os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) sys.path.append(BASE ...
- Python中的元类
从前面"Python对象"文章中了解到,在Python中一切都是对象,类可以创建实例对象,但是类本身也是对象. class C(object): pass c = C() prin ...
- Tomcat中catalina run后台运行脚本
编写启动脚本start.sh,将其放在/srv/aubapp/bin/下 #!/bin/sh #设置web应用程序目录 export CATALINA_BASE="/srv/aubapp&q ...
- jenkins checkstyle:local variable hides a field
源代码: 1 2 3 4 5 6 7 8 //应用上下文 private static ApplicationContext applicationContext; public static voi ...
- V$SQLAREA
1.查看消耗资源最多的SQL: SELECT hash_value, executions, buffer_gets, disk_reads, parse_calls FROM V$SQLAREA W ...
- mysql的日志及利用mysqldump备份及还原
日志文件:6类 一般查询日志:log,general_log,log_output 慢查询日志: 错误日志 二进制日志 中继日志 ...
- centos7 Firewalld操作集合
=============================================== 2019/4/15_第1次修改 ccb_warlock == ...
- Java连接oracle数据库的两种常用方法
1. 使用thin连接 由于thin驱动都是纯Java代码,并且使用TCP/IP技术通过java的Socket连接上Oracle数据库,所以thin驱动是与平台无关的,你无需安装Oracle客户端,只 ...