【TopCoder】SRM160 DIV1总结
做了两道题之后才发现做的是DIV1,不是DIV2,DIV1的第二道题是DIV1的第三道题,果断决定第3题就不看了=。=
250分题:给定一个时间起点8:00 AM DAY 1,再给出一组时间终点,格式是hh:mm xM, DAY n,要求计算每一组起点终点形成的时间段长度的均值,以分钟为单位。
Problem Statement
The Iditarod is a dogsled race from Anchorage to Nome that takes many days. We want to take a list of the times when the competitors crossed the finish line and convert that into the average number of minutes to complete the race.
The race starts on day 1 at 8:00 AM. We are given a list of finish times as a String[], where each finish time is formatted as hh:mm xM, DAY n
where hh is exactly 2 digits giving the hour, mm is exactly 2 digits giving the minute, x is either 'A' or 'P', and n is a positive integer less than 100 with no leading zeros. So each string has exactly 15 or 16 characters (depending on whether n is less than 10). Create a class Iditarod containing method avgMinutes that is given a String[], times, and that returns the average number of minutes taken by the competitors to complete the race. Round the returned value to the nearest minute, with .5 rounding up.
上次做了一道题,也是跟时间有关,学会了一个实用的技巧,就是遇到这种时间段的问题就把hh:mm:ss这种格式全部转换成一个数值,比如这道题的时间起点是8:00 AM DAY 1,但是如果以这个时间为起点很不方便,于是就以0:00 AM DAY 1作为起点,那么8:00 AM DAY 1对应的数值就是8*60=480,而
hh:mm AM, DAY n对应的数值就是(n-1)*24*60+hh*60+mm(如果hh是12,要把它置为0,因为它代表午夜12点);
hh:mm PM, DAY n对应的数值就是(n-1)*24*60+(hh+12)*60+mm(如果hh是12,也要把它置0,因为它代表中午12点,hh+12=0+12才是中午12点);
这样两个时间点对应的时间段就是他们对应的数值之差了,比如样例(0)中
12:00 PM, DAY 1对应的数值是(1-1)*24*60+(0+12)*60+0=720,它与起始时间所差的分钟数是720-480=240;
同理12:01 PM, DAY 1对应的数值是721,与8:00 AM DAY 1所差的分钟数是241;
那么它们的时间段平均值就是(240+241)/2=240.5,四舍五入后得到241。
代码:Iditarod
500分题:这个其实是DIV2的1000分题的,给定一组字符串,每个字符串代表一种颜色,然后从最中间的位置开始,按照逆时针螺旋方向绕行“织被子”,每个位置上选择颜色的时候有三个约束条件,最终需要完成一个width*height大小的被子,我还是把题目贴出来吧:
Problem Statement
A quilt is made by sewing square patches of different colors together in a pattern. We are using a pattern that says to start with one patch, and then add patches starting with the patch above it and continuing by spiraling outward counterclockwise until we have the desired size. The picture below shows the order of the patches (a then b then c etc.) in crafting a quilt whose length(i.e. height) is 4 and whose width is 3.
lkj
cbi
dah
efg Define the neighbors of a newly added patch to be all the previous patches that touch the new patch (including those that just touch diagonally at a corner). The rules for choosing the color of the newly added patch are 1) choose a color that minimizes the number of neighbors of the same color
2) choose a color that has been used least often by previous patches
3) choose the earliest(lowest index) color in the colorList
Rule 2 is applied only to decide among colors that are tied after rule 1 has been applied. Rule 3 is applied only to decide among colors that are tied after the first two rules have been applied. Create a class Quilting that contains a method lastPatch that returns the color of the last patch added to the quilt. Its inputs are an int length and an int width (the two dimensions of the finished quilt), and a String[] colorList giving the available colors. length minus width will be 0 or 1, so it will always be possible to produce a quilt of the given size.
这题有两个关键点,一是怎么实现螺旋逆时针绕行,官网这里解释的很清楚了,通过两个数组int[] dx = {0,-1,0,1};int[] dy = {-1,0,1,0};和三个变量dir,gone,toGO来实现。两个数组对应位置的四个变量分别代表了向下,向左,向上,向右四个方向移动,通过当前坐标(x,y)加上dx,dy中对应的值就可以实现坐标移动了。dir就是记录当前向哪里移动的,它的更新由dir=(dir+1)%4实现;而gone和toGo分别表示在某个方向上已经走了多远和需要走多远。如下图所示:
假设现在位于红色的点那里,接下来那么gone=2,表示已经走了两部了,但是toGo=4,表示在这个方向上要走4步,还要继续走两步。
找规律发现toGo初始为1,在dir=0和dir=2的时候分别加1,而gone则是每次加到等于toGo的时候就置零,表示转弯了。
第二个关键点是怎么根据三个约束条件选择下一个颜色,用一个数组nb来遍历某个元素周围九个格子(如果有)的颜色情况,一个数组used来存放各个颜色已经被使用过的次数,那么就可以遍历所给定的颜色数组,查找合适的颜色,注意这里第三个要求是自动满足的,因为选择合适颜色的时候是从前往后遍历颜色数组的,所以前面的颜色会被优先选择,而前两个条件,则分别用nb和used数组来满足。
代码:Quilting
【TopCoder】SRM160 DIV1总结的更多相关文章
- TopCoder 649 div1 & div2
最近一场TC,做得是在是烂,不过最后challenge阶段用一个随机数据cha了一个明显错误的代码,最后免于暴跌rating,还涨了一点.TC题目质量还是很高的,非常锻炼思维,拓展做题的视野,老老实实 ...
- TopCoder SRM500 Div1 250 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-250.html SRM500 Div1 250 题意 (看题用了半个小时--) 有 n 个人(编号 ...
- TopCoder SRM500 Div1 500 分治
原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-500.html SRM500 Div1 500 没想到 double 的精度居然没有爆-- 考虑以 ...
- TopCoder SRM500 Div1 1000 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-1000.html SRM500 Div1 1000 设 \(v_1,v_2,\cdots ,v_9 ...
- TopCoder SRM502 Div1 500 贪心 01背包
原文链接https://www.cnblogs.com/zhouzhendong/p/SRM502-500.html SRM502 Div1 500 好题. 首先,如果已经确定了解决所有问题的优先级, ...
- TopCoder SRM502 Div1 1000 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/SRM502-1000.html SRM502 Div1 1000 题意 从 [0,n-1] 中选择 k 个不同的 ...
- topcoder SRM712 Div1 LR
题目: Problem Statement We have a cyclic array A of length n. For each valid i, element i-1 the l ...
- TopCoder 603 div1 & div2
div2 250pts MiddleCode 题意:s串长度为奇数时,将中间字符取掉并添加到t末尾:长度为偶数时,将中间两个较小的字符取掉并添加到末尾. 分析:直接做,学习了一下substr(s, p ...
- TopCoder SRM704 Div1 800 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/SRM704-800.html 题解 考虑构造一个 $n = 20$ 的图. 先把所有 $i$ 都连向 $i-1$ ...
随机推荐
- CF 482A(Diverse Permutation-相邻距离不同数为k的1~n全排列构造)
A. Diverse Permutation time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces 455A Boredom 取数字的dp
题目链接:点击打开链接 给定一个n长的序列 删除x这个数就能获得x * x的个数 的分数,然后x+1和x-1这2个数会消失.即无法获得这2个数的分数 问最高得分. 先统计每一个数出现的次数.然后dp一 ...
- 使用shell脚本守护node进程
现在开源的守护node进程的包有不少,比如forever,pm2,这里我就不再赘述了. 但是有的公司生产服务器是不能联网的,而这些包都需要全局安装,必须要网络环境.难道你nohup node app. ...
- ASP.NET动态网站制作(27)-- 三层框架(1)
前言:今天主要介绍一下三层框架,给大家一个整体的概念.分层概念使得程序低耦合,更加健壮,扩展性更好. 内容: 1.三层: UI(表现层):主要是指与用户交互的界面.用于接收用户输入的数据和显示处理后用 ...
- OpenCV学习笔记十三:opencv_videostab模块
一,简介: 该库用于视频稳像.
- php 代码的执行
PHP内核的实现和世界上绝大数的程序一样,接收输入数据,做相应处理后输出结果.用PHP编写的代码就是输入数据,PHP内核对编写的代码进行解释和运算,最后返回运算结果.当编写的PHP代码给内核去执行的时 ...
- Android ADB 命令链接模拟器出现 daemon not running 解决方法
用adb命令链接远程模拟器 有时候会遇到如下问题: C:Documents and SettingsAdministrator>adb connect 192.168.0.183 * daemo ...
- 锁(java, DB)
知识点 事务 锁(java, DB) 多线程知识点整理 锁(java, DB) 什么是锁 对资源的访问权限进行控制 如果把一个资源(对象)比喻成屋子.就好像你进入了屋子锁上了门.你家人和贼都进不去了. ...
- scaffolding —— 脚手架(转)
Scaffolding — 基架 基于数据库架构生成网页模板的过程.在 ASP .NET 中,动态数据使用基架来简化基于 Web 的 UI 的生成过程.用户可以通过这种 UI 来查看和更新数据库. ...
- 160817、Java数据类型以及变量的定义
Java 是一种强类型的语言,声明变量时必须指明数据类型.变量(variable)的值占据一定的内存空间.不同类型的变量占据不同的大小. Java中共有8种基本数据类型,包括4 种整型.2 种浮点型. ...