做了两道题之后才发现做的是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总结的更多相关文章

  1. TopCoder 649 div1 & div2

    最近一场TC,做得是在是烂,不过最后challenge阶段用一个随机数据cha了一个明显错误的代码,最后免于暴跌rating,还涨了一点.TC题目质量还是很高的,非常锻炼思维,拓展做题的视野,老老实实 ...

  2. TopCoder SRM500 Div1 250 其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-250.html SRM500 Div1 250 题意 (看题用了半个小时--) 有 n 个人(编号 ...

  3. TopCoder SRM500 Div1 500 分治

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-500.html SRM500 Div1 500 没想到 double 的精度居然没有爆-- 考虑以 ...

  4. TopCoder SRM500 Div1 1000 其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-1000.html SRM500 Div1 1000 设 \(v_1,v_2,\cdots ,v_9 ...

  5. TopCoder SRM502 Div1 500 贪心 01背包

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM502-500.html SRM502 Div1 500 好题. 首先,如果已经确定了解决所有问题的优先级, ...

  6. TopCoder SRM502 Div1 1000 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM502-1000.html SRM502 Div1 1000 题意 从 [0,n-1] 中选择 k 个不同的 ...

  7. topcoder SRM712 Div1 LR

    题目: Problem Statement      We have a cyclic array A of length n. For each valid i, element i-1 the l ...

  8. TopCoder 603 div1 & div2

    div2 250pts MiddleCode 题意:s串长度为奇数时,将中间字符取掉并添加到t末尾:长度为偶数时,将中间两个较小的字符取掉并添加到末尾. 分析:直接做,学习了一下substr(s, p ...

  9. TopCoder SRM704 Div1 800 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM704-800.html 题解 考虑构造一个 $n = 20$ 的图. 先把所有 $i$ 都连向 $i-1$ ...

随机推荐

  1. Razor 3、MVC 5

    Razor 3 需要vs 2012 update 4 才可以 需要装一个 Microsoft ASP.NET and Web Tools 2013.1 才会有 MVC 5

  2. 6.5安装nagios

    最近因为,科研需要,接触上了Nagios,这里,我将安装笔记做个详解.为自己后续需要和博友们学习! VMware workstation 11 的下载 VMWare Workstation 11的安装 ...

  3. 更新换代----systemctl命令取代chkconfig和service

    systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 ...

  4. Oracle----oracle小知识总结

    1,表列的五种约束 not null, unique,primary key, foreign key, check 2,权限分配 grant 权限 on 表 to 用户 3,表和视图的区别 视图是一 ...

  5. 第一百八十四节,jQuery-UI,验证注册表单

    jQuery-UI,验证注册表单 html <form id="reg" action="123.html" title="会员注册" ...

  6. ps -ef | grep java 查看所有关于java的进程

    ps -ef | grep java   查看所有关于java的进程

  7. 转载:tar 解压缩命令~

    转载自:http://blog.csdn.net/dunyanan1/article/details/38869059tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文 ...

  8. 庖丁解牛:USB 驱动开发技术彻底解密

    我们知道如果开发工程师不懂RS232 肯定会让人笑话可以想象面向未来USB 接口无处不在因此掌握USB 的原理固件编程及其驱动开发技术势必成为当务之急USB 即插即用的优点和灵活性运用于各种电子产品现 ...

  9. redis 集群出现的错误

    1 解决方法: 不用 Jedis jed =new jedis("192.168.56.101"); jed.set(key,value); 用 Set<HostAndPor ...

  10. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.4——Flavor Dimensions

    问题: 一个product flavor不够,你需要另一个标准去区分不同版本的app 解决方案: 在product flavor中增加flavorDimensions 讨论: 在3.2章展示了一个有三 ...