BNU 26579 Andrew the Ant 【蚂蚁】
链接:
Andrew the Ant
%lld Java class name:
Main
+
-
None
Graph Theory
2-SAT
Articulation/Bridge/Biconnected Component
Cycles/Topological Sorting/Strongly Connected Component
Shortest Path
Bellman Ford
Dijkstra/Floyd Warshall
Euler Trail/Circuit
Heavy-Light Decomposition
Minimum Spanning Tree
Stable Marriage Problem
Trees
Directed Minimum Spanning Tree
Flow/Matching
Graph Matching
Bipartite Matching
Hopcroft–Karp Bipartite Matching
Weighted Bipartite Matching/Hungarian Algorithm
Flow
Max Flow/Min Cut
Min Cost Max Flow
DFS-like
Backtracking with Pruning/Branch and Bound
Basic Recursion
IDA* Search
Parsing/Grammar
Breadth First Search/Depth First Search
Advanced Search Techniques
Binary Search/Bisection
Ternary Search
Geometry
Basic Geometry
Computational Geometry
Convex Hull
Pick's Theorem
Game Theory
Green Hackenbush/Colon Principle/Fusion Principle
Nim
Sprague-Grundy Number
Matrix
Gaussian Elimination
Matrix Exponentiation
Data Structures
Basic Data Structures
Binary Indexed Tree
Binary Search Tree
Hashing
Orthogonal Range Search
Range Minimum Query/Lowest Common Ancestor
Segment Tree/Interval Tree
Trie Tree
Sorting
Disjoint Set
String
Aho Corasick
Knuth-Morris-Pratt
Suffix Array/Suffix Tree
Math
Basic Math
Big Integer Arithmetic
Number Theory
Chinese Remainder Theorem
Extended Euclid
Inclusion/Exclusion
Modular Arithmetic
Combinatorics
Group Theory/Burnside's lemma
Counting
Probability/Expected Value
Others
Tricky
Hardest
Unusual
Brute Force
Implementation
Constructive Algorithms
Two Pointer
Bitmask
Beginner
Discrete Logarithm/Shank's Baby-step Giant-step Algorithm
Greedy
Divide and Conquer
Dynamic Programming
Tag it!
Andrew the Ant is fascinated by the behavior of his friends. Thousands of them are marching their paths on and on. They can build highly organized ant-hills. Sometimes, however, they act a little bit stupidly.
Recently, Andrew watched his fellow ants marching on top of a long piece of wood. He noticed their behavioral pattern is very simple: Each ant walks slowly forward with a constant speed of 1 cm per second. Whenever it meets another ant, both of them only touch with their antennae and immediately turn around and walk the opposite direction. If an ant comes to the end of the wood, it falls down and does not affect other ants anymore.

The picture above shows an example of moving ants in time 0 s. In one second, the ants E and A meet at position 2 and change their directions. The ant A then meets B in the next 1.5 seconds. At the same time (2.5 seconds after the start), the ants C and D will meet too. All four of them change their directions. In the next 0.5 second (time 3 s), the first ant (E) falls down off the left end, etc.
Your task is to simulate the movement of ants. For simplicity, suppose that the ants have zero size (although the picture could suggest something else).
Input
The input consists of several scenarios. Each scenario starts with a line containing two integer numbers L and A, separated by a space. L is the length of the wood in cms (1 ≤ L ≤ 99 999), and A is the number of ants at the beginning of the simulation (1 ≤ A ≤ L + 1).
Then there are A lines, each containing a positive integer Xi, one space, and an uppercase letter. The number (0 ≤ Xi ≤ L) specifies the position of the i-th ant and the letter its initial direction: either “L” for left (towards zero) or “R” for right. No two ants will start at the same position.
Output
For each scenario, you should print a single line containing the text “The last ant will fall down in T seconds - started at P.”, where T is the exact time when the last ant (or two) reaches the end of the wood, and P is the position where that particular ant has originally started in time 0. If two last ants fall down at the same time, print “started at P and Q”, indicating both of their positions, P <Q.
Sample Input
90000 1
0 R
10 1
0 L
14 5
3 L
6 L
13 L
8 R
1 R
Sample Output
The last ant will fall down in 90000 seconds - started at 0.
The last ant will fall down in 0 seconds - started at 0.
The last ant will fall down in 13 seconds - started at 6 and 8.
Hint
(The last sample input scenario corresponds to the picture.)
Source
参考资料:lrj 《算法竞赛入门经典——训练指南》
题意:
(1 ≤ L ≤ 99 999)
(1 ≤ N ≤ L + 1).
思路:
注意:
由题目样例知道:初始在边缘的而且往木棍外面走的蚂蚁最终下落时间记为 0
所以计算的时间 T 就按照最后一只到了边缘的蚂蚁用的时间 T 计算, 而不是 T-1。
code:
#include<stdio.h>
#include<algorithm>
using namespace std; const int maxn = 99999+10; struct Ant{
int id; //输入顺序
int p; //位置
int d; //朝向。-1:左; 0:转身中;1:右
bool operator < (const Ant& a) const { //按照 p 从左到右排序
return p < a.p;
}
}before[maxn], after[maxn]; //初始状态和最终状态 int order[maxn]; //输入的第 i 只蚂蚁是最终状态中的左数第 order[i] 只蚂蚁 struct Node{ //记录初始输入状态
int p;
int d;
}node[maxn]; int id[2];//记录最后下落的蚂蚁的编号 int main()
{
int L,n; //木棍长度和蚂蚁个数
while(scanf("%d%d", &L,&n) != EOF)
{
int T = 0; for(int i = 0; i < n; i++)
{
int p, d;
char c;
scanf("%d %c", &p, &c);//输入因为第二个是字符注意添加空格
d = (c == 'L' ? -1 : 1); int t;
if(d == -1) t = p;
if(d == 1) t = L-p; T = max(T, t); //最后一只蚂蚁下落时间【正好到边缘】 node[i] = (Node) {p, d};
} for(int i = 0; i < n; i ++)
{
int p = node[i].p;
int d = node[i].d; before[i] = (Ant) {i, p, d}; //初始状态
after[i] = (Ant){0, p+T*d, d}; //最终状态, id 未知, 可以忽略 d
} //计算 order 数组
sort(before, before+n); //按照蚂蚁的初始位置排序, 并未改变其 id
for(int i = 0; i < n; i++)
order[before[i].id] = i; //输入的第 i 只蚂蚁是最终状态中的左数第 order[i] 只蚂蚁 //计算最终状态:按照蚂蚁位置从左到右排序
sort(after, after+n); int index = 0; //记录最后一刻下落的蚂蚁个数
for(int i = 0; i < n; i++)
{
int a = order[i];
if(after[a].p == 0 || after[a].p == L) id[index++] = i; //正好到边缘
}
if(index == 1) //如果只有一只蚂蚁
{
int p1 = node[id[0]].p;
printf("The last ant will fall down in %d seconds - started at %d.\n", T, p1);
} else if(index == 2) //如果有两只蚂蚁,按照初始位置从左到右输出
{
int p1 = min(node[id[0]].p, node[id[1]].p);
int p2 = max(node[id[0]].p, node[id[1]].p);
printf("The last ant will fall down in %d seconds - started at %d and %d.\n", T, p1, p2);
}
}
return 0;
}
总结:
第三场组队赛了,还是太过于浮躁,第一个 AC 的题是一道简单的 DP 一年前就做过的,很快的明确了思路,开始敲,这时Orc 在看蚂蚁的这一题。结果没有敲完就把编译器给弄坏了,然后把代码交个 Orc 各种改,我就各种安装编译器,最终 cb 还是弄坏了,只好装上很久以前用的 cfree 各种不习惯,还好这个时候 Orc 已经把 DP 改出来了,由于 int64 和 long long 问题 WA 了一次,马上AC了。已经过了一个小时了。排第6。。。各种悲催!
然后 Orc 对我说 D 题是计算几何很多人过了,我马上去看 D 他继续看蚂蚁的这题。我看了下发现 D 不是计算几何而是简单的 BFS 才一百个点,但是没有看清楚题目中的英文,暂时猜测了下方向和骑士周游世界是一样的,然后我开始以这个思路敲,同时让Orc 也看看这题,方向是不是和我猜测的一样。然后很快就过了样例!问了下 Orc 决定交。惨淡的 TLE 。。。这可是 100 个点的 BFS 啊。。。。。。。然后让Orc检查,然后我发现是初始标记起点入队写错了,改了还是 TLE (此时贡献了两次TLE了)然后就是各种检查,各种数据测试,都没问题,最终快到 2 点的时候 Orc 发现我在输入的时候掉了 != EOF
检查了那么久都没有发现问题啊!!!
看了下排行榜这时两题第三吧,要是不白交两次 TLE 就应该是第二了。然后最后一题分巧克力的看有人过了,Orc猜测应该比较简单,但是我实在是看不懂题目就没有管了,然后看了下 F 有人过了,看了输入和输出发现和斐波拉契生兔子的有点像,再仔细看了下题目背景,发现是一水题,很快 AC 了,这时 2:49 了。3题排第三吧,学妹那支队伍这几次都表现的比较好,一直在我们前面,不是第一就是第二。然后又回来看最后一题,最终是 Orc 看懂了题目,然后我各种 YY 还是没有枚举分析完,比赛完了,Orc看了下题解说是斐波拉契。。。然后我看了下 B 的字符串 WA 的人很多,自己也没法下手,然后又去看了下 A 就是这道蚂蚁的题目,也发现了转换问题。。。正式开始写的时候只有半个小时了,很快写完了,但是计算时间的时候我是输入完了之后在计算 T 的,然后写 d 的时候不是算的每一个蚂蚁的,写错了,算成了最后一只蚂蚁的,样例和自己的数据都无法查出错误。。。。然后就这样 WA 到了比赛完。最后一题很多人做我们又没有写出来,最终 3 题排第 5 弱爆了,被两只老队伍踩外加两支新队伍踩。。。
难题做不出,简单题读题速度不够快,写简单题目的时候心态又不够平和,各种小错误,导致很久才能 AC 。
lrbj 回家了,每次还好有 Orc 来改错,然后基本上都是他先 AC 题目,避免了暴 0 的情况,比的心里也有点底吧。比赛基本上都看不到计算几何的题目,等 lrbj 来了三人合作情况应该会好一点,至少罚时什么的不会这么多了吧。看英文题目的速度会快一点,然后基础题目 AC 的速度应该也会快点。现在才越来越体会到合作的重要性。不过我们两个虽然每次敲代码的时候只用了一台电脑,但是却用了两台电脑看题目,也是自己单独敲的,这种习惯很不好,正式比赛一台电脑肯定是不会适应的了。
BNU 26579 Andrew the Ant 【蚂蚁】的更多相关文章
- BNUOJ 26579 Andrew the Ant
LINK:Andrew the Ant 题意:给一根长度为L的木头,上面有A只蚂蚁[每只蚂蚁给出了初始行走的方向,向左或向右].当两只蚂蚁相碰时,两只蚂蚁就朝相反的方向行走~╮(╯▽╰)╭问的是:最后 ...
- 前端ui框架---ant 蚂蚁金服开源
蚂蚁金服和饿了么好像不错 饿了么官网:http://element.eleme.io/#/zh-CN饿了么github:http://github.com/elemefe 蚂蚁金服 https:// ...
- 报名 | 蚂蚁金服ATEC科技大会 · 上海:数字金融新原力
小蚂蚁说: 2019年1月4日,蚂蚁金服ATEC城市峰会将以“数字金融新原力(The New Force of Digital Finance)”为主题,在中国上海举办.蚂蚁金服ATEC(Ant Te ...
- P2586 [ZJOI2008]杀蚂蚁
传送门 快乐模拟,修身养性 代码长度其实还好,主要是细节多 只要知道一些计算几何基础知识即可快乐模拟,按着题目要求一步步实现就行啦 注意仔细读题,蚂蚁每 $5$ 秒乱走一次的时候是只要能走就走了,不一 ...
- Java实现蓝桥杯历届试题兰顿蚂蚁
历届试题 兰顿蚂蚁 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 兰顿蚂蚁,是于1986年,由克里斯·兰顿提出来的,属于细胞自动机的一种. 平面上的正方形格子被填上黑色或白色.在其 ...
- 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法
若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...
- react相关
react 简单入门 ant 蚂蚁金服react组件 redux 阮一峰入门react material-ui组件库 webpack入门 http://www.jianshu.com/p/42e115 ...
- 蚁群算法(Java)tsp问题
1.理论概述 1.1.TSP问题 旅行商问题,即TSP问题(旅行推销员问题.货郎担问题),是数学领域中著名问题之一.假设有一个旅行商人要拜访n个城市,他必须选择所要走的路径,路径的限制是每个城市只 ...
- CSS3 opacity
opacity用来设置元素的透明度. 值被约束在[0.-1.0]范围内,如果超过了这个范围,其计算结果将截取到与之最相近的值. 0表示完全透明,1表示完全不透明. 浏览器支持: (1).IE浏览器支持 ...
随机推荐
- cocos2d-x中的尺寸之一
cocos2d-x中的尺寸函数繁多,官方文档对各种尺寸没有很好的描述,网上文章更加寥寥,对尺寸和位置的理解如果不到位,写代码就非常困难,需要猜测尝试,效率低下.这个文章我将研究一些我所迷惑的尺寸函数, ...
- 盘点:移动服务 #AzureChat
感谢大家帮助我们顺利推出史无前例的 #AzureChat.移动服务和 Notification Hub 是 Windows Azure 平台上令人振奋的服务.我们很高兴能借这次在线讨论的机会,倾听各位 ...
- UBER司机奖励政策
高峰时段: 早高峰:早6:30-8:30点 晚高峰:晚4:00-7:00点 *周六日只有晚高峰 其他时间均为平峰时段 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注 ...
- Node.Buffer
介绍 Buffer是一个典型的javascript与c++结合的模块,它将性能相关的部分用c++实现,将非性能相关的部分用javascript实现. 纯 JavaScript 对 Unicode 友好 ...
- 【PAT】1009. Product of Polynomials (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...
- HTTP协议是无状态协议,怎么理解?
Http是一个无状态协议,同一个会话的连续两个请求互相不了解,他们由最新实例化的环境进行解析,除了应用本身可能已经存储在全局对象中的全部信息外,该环境不保存与会话有关的不论什么信息. 自己的理解,在 ...
- Linux升级Python提示Tkinter模块找不到解决
一.安装tkinter 在Linux中python默认是不安装Tkinter模块, [root@li250- ~]# python Python (r266:, Feb , ::) [GCC (Red ...
- DevExpress ASP.NET 使用经验谈(6)-ASPxGridView属性设置与CRUD界面优化
上一节中,我们通过简单的配置,通过ASPxGridView控件的使用,完成了对数据库表的CRUD操作. 这样的界面展现,功能是达到了,但是操作体验上,还是有所欠缺的. 图一 默认生成的列表界面 图二 ...
- JSP内置对象---application
application 对象 服务器启动后,就产生了application 对象.当一个客户访问服务器上的一个JSP 页面时,JSP 引擎为该客户分配这个application 对象, 当客户在 ...
- php随笔11-Thinkphp常用系统配置大全
Thinkphp常用配置 CHECK_FILE_CASE -- windows环境下面的严格检查大小写. /* 项目设定 */ 'APP_DEBUG' => false, // ...