【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$ ...
随机推荐
- 探讨电子商务新的商业模式O2O
在当前的市场经济大环境下,电子商务成为了众 多中小企业用以开拓市场的主要工具,成为突破企业管理瓶颈的重要手段.必卖电子商务顾问认为,电子商务应该是是线上业务与线下业务的结合.对于从线下渠道 起家的传统 ...
- ssh 面试
Struts1工作原理1. 初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总 控制器会读取配置文件(st ...
- QQ宠物怀念版H5
<QQ宠物>相伴十三载 哇!明信片邮到了!!开心ヽ(○^㉨^)ノ♪ 大乐斗Ⅱ 怀念一波...
- 程序运行缺少MSVCR110.dll
Download Visual C++ Redistributable for Visual Studio 2012 Update 4 from Official Microsoft Download ...
- xshell ftp报错:找不到匹配的outgoing encryption算法
场景:由于登陆跳板机都是从采用密钥的方式进行登陆的,然后在传输文件的时候报错 报错信息: 解决方案: 点击属性--->选择aes256-ctr加密方式默认这里是没有选择的 再次连接就成功连接上去 ...
- redhat 配置本地yum源163yum源epel 源,无需卸载yum!无须拷贝ISO
都知道redhat不收费,但是其yum服务是要收费的,不想出钱那就自己配置yum源就好了. 首先,博主之前也没用过redhat,第一次用yum装包的时候提示什么没注册之类的,balaba一大堆,然后就 ...
- 1674 区间的价值 V2(分治)
1674 区间的价值 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 lyk拥有一个区间. 它规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有 ...
- golang 模板(template)的常用基本语法
1. 模板 在写动态页面的网站的时候,我们常常将不变的部分提出成为模板,可变部分通过后端程序的渲染来生成动态网页,golang提供了html/template包来支持模板渲染. 这篇文章不讨论gola ...
- do while 宏定义如何使用?
#define verification(lhs,rhs,str) do { \ if (lhs == rhs) { \ std::cout << rang::style::bold &l ...
- TGraphicControl(自绘就2步,直接自绘自己,不需要调用VCL框架提供的函数重绘所有子控件,也不需要自己来提供PaintWindow函数让管理框架来调用)与TControl关键属性方法速记(Repaint要求父控件执行详细代码来重绘自己,还是直接要求Invalidate无效后Update刷新父控件,就看透明不透明这个属性,因为计算显示的区域有所不同)
TGraphicControl = class(TControl) private FCanvas: TCanvas; procedure WMPaint(var Message: TWMPaint) ...