做了两道题之后才发现做的是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. JIRA /mnt/server/atlassian-jira-6.3.6-standalone/bin/start-jira.sh

    JIRA 敏捷开发平台部署记录   分类: 敏捷开发   1.1 jira说明 JIRA是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪. ...

  2. WPF数据模板样式选择器

    在使用数据模板样式选择器时,不能设置ItemContainerStyle的属性值,如果设置了该值,那么数据模板样式选择器会失去作用. 在使用数据模板样式选择器时,首先要创建数据模板样式选择器对象,此对 ...

  3. 巧妙使用JQuery Clone 添加多行数据,并更新到数据库的实现代码

    web前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="BatchAd ...

  4. python静态网页爬虫之xpath

    常用语句: 1.starts-with(@属性名称,属性字符相同部分)使用情形: 以相同的字符开头 <div id = 'test-1'>需要的内容1</div> <di ...

  5. Ninject学习笔记<三>

    ASP.NET MVC学前篇之Ninject的初步了解 1.介绍 废话几句,Ninject是一种轻量级的.基础.NET的一个开源IoC框架,在对于MVC框架的学习中会用到IoC框架的,因为这种IoC开 ...

  6. 导出excel页面假死

    如果是asp.net页面 public override void VerifyRenderingInServerForm(Control control)    {} 如果是Sharepoint w ...

  7. 从网上搜索到的一些关于pcap源代码,入门级的

    /*pcap_1.c*/ #include <stdio.h>#include <stdlib.h>#include <pcap.h>  /* 如果没有pcap的系 ...

  8. c# 如何利用异或运算进行简单加密解密

    利用“^”异或运算对字符串进行加密 原理:按位做“异或”运算是->位值相同得1,不同得0,如下计算 1 ^ 1 = 0 1 ^ 0 = 1 0 ^ 1 = 1 0 ^ 0 = 0 例如: < ...

  9. JavaScript基础深入之

    JS的数值类型是分为两类:基本数据类型和引用数据类型. 基本类型占据的内存栈空间,引用类型被保存在堆空间.引用类型赋值的变量也是被保存在栈空间的,它的作用类似于电视遥控器,负责操作堆空间内指向的对象. ...

  10. CSS如何清除浮动流的多种方案

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...