[Code Jam] Theme Park
来源:互联网 发布:飞艇计划 软件下载 编辑:程序博客网 时间:2024/04/29 02:01
可以看到 Large dataset 中的数量级达到了10^8,因此如果循环R次的话,程序可能会瘫痪。
因此这里再次强调了对数量级的把握,以及存储类型的选择,包括算法的选择。
int类型的最大值是2147483647,也就是达到了10^9。
long类型的最大值是9223372036854775807,也就是10^18。
因此对于 Large dataset,还是采用long保险一点,因为有可能结果N*R相乘以后突破范围,出现负值或者报错。
另外,推荐使用for循环,尽量不用while循环。任何的while循环都可以用for循环表示,查找错误时往往也更容易。
在Java中全部代码如下:
有以下几点需要注意:
1. 在写核心算法的时候很关键的一个地方在于首次重复出现的范围,引用 Contest Analysis 中的一段话:
“It turns out that a cycle must show up within the first N+1 rides, because there are only N different states the queue can be in (after N, you have to start repeating). So you only have to simulate N rides, each of which takes O(N) time in the worst case, before finding the cycle: that's an O(N2) solution. ”
因此如果用k来表示状态的个数的话,那么循环的终止条件就是k<N+1,即存储N+1个状态。这N+1个状态中,必然至少有两个是相等的。
2. 排序和查找是数组运算中最为重要的两种操作,我们可以依靠Arrays类来实现它们。
分别调用sort()方法和binarySearch()方法,但是注意二分查找方法的使用前提是针对有序数组。
也就是在使用之前数组要用sort排过序,否则可能会出现错误结果。
本题中由于需要得到每个元素的序号,因此排序是不可行的,所以干脆直接写一个search()函数即可,返回序号,不用几行就搞定。
- [Code Jam] Theme Park
- Google Code Jam 2006
- Google China Code Jam
- code.jam - pixelcanvas
- code.jam - egg drops
- Google Code Jam
- [Code Jam] Alien Language
- 关于算法、code jam
- [Code Jam] Watersheds
- [Code Jam] Fair Warning
- Code Jam练习
- Google Code Jam 130914
- Code jam beta 2008
- [Code Jam] Crazy Rows
- [Code Jam] Millionaire
- Code Jam--Good Luck
- Code Jam--The Repeater
- Google Code Jam 2015
- Learning Python
- Gridview自定义排序且显示上下箭头
- OIPF 规范翻译(DAE)-----4.4.5 Media control
- 深入探索Factory模式与Prototype模式的异同
- PrintShield打印监控系统
- [Code Jam] Theme Park
- [转]VC++ Tab Control控件的使用
- Great findings
- UV光的应用
- OIPF,源于电信阵营的开放IPTV 解决方案
- learning git
- WinShield内网管理系统
- 剖析nWE, nWBE, nBE三者之间的关系
- asterisk有关的资料