做了两道题之后才发现做的是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。
代码:
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 color2) choose a color that has been used least often by previous patches3) choose the earliest(lowest index) color in the colorListRule 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数组来满足。
代码: