Problem Statement
????
Note that in the following problem statement, all quotes and angle brackets are for clarity
A certain vending machine delves out its goods from a rotating cylinder, which can rotate around in both clockwise and counter-clockwise directions. The cylinder has a number of shelves on it, and each shelf is divided into a number of columns. On the front of the machine, there is a panel of doors that extends the entire height of the column. There is one door for each shelf, which is the width of one column. When a purchase is made, the user uses two buttons to rotate the cylinder so their purchase is located at a door. They make their purchase by sliding the appropriate door open, and removing the item (there can only be one item per column on a particular shelf). The cylinder can rotate in a complete circle, and so there are always two ways to get from a particular column to another column.
Because the vending machine company wants to sell the most expensive items possible, and the machine can only show one column at a time, the machine will always try to put forth the most expensive column available. The price of a column is calculated by adding up all the prices of the remaining items in that column. The most expensive column is defined to be the one with the maximum price. If 5 minutes have elapsed since the last purchase was made, the machine rotates the cylinder to the most expensive column. If, however, another purchase has been made before the 5 minutes are up, the rotation does not occur, and the 5 minute timer is reset.
Recently, some machines' rotating motors have been failing early, and the company wants to see if it is because the machines rotate to show their expensive column too often. To determine this, they have hired you to simulate purchases and see how long the motor is running.
You will be given the prices of all the items in the vending machine in a String[]. Each element of prices will be a single-space separated list of integers, which are the prices (in cents) of the items. The Nth integer in the Mth element of prices represents the price of the Nth column in the Mth shelf in the cylinder. You will also be given a String[] purchases. Each element in purchases will be in the format: "<shelf>,<column>:<time>" <shelf> is a 0-based integer which identifies the shelf that the item was purchased from. <column> is a 0-based integer which identifies the column the item was purchased from. <time> is an integer which represents the time, in minutes, since the machine was turned on.
In the simulation, the motor needs to run for 1 second in order to rotate to an adjacent column. When the machine is turned on, column 0 is facing out, and it immediately rotates to the most expensive column, even if the first purchase is at time 0. The machine also rotates to the most expensive column at the end of the simulation, after the last purchase. Note that when an item is purchased, its price is no longer used in calculating the price of the column it is in. When the machine rotates to the most expensive column, or when a user rotates the cylinder, the rotation is in the direction which takes the least amount of time. For example, in a 4-column cylinder, if column 0 is displayed, and the cylinder is rotated to column 3, it can be rotated backwards, which takes 1 second, versus rotating forwards which takes 3 seconds.
If a user tries to purchase an item that was already purchased, this is an incorrect simulation, and your method should return -1. Otherwise, your method should return how long the motor was running, in seconds.
Definition
????
Class:
VendingMachine
Method:
motorUse
Parameters:
String[], String[]
Returns:
int
Method signature:
int motorUse(String[] prices, String[] purchases)
(be sure your method is public)
????
Notes
-
When rotating to the most expensive column, if two columns have the same price, rotate to the one with the lowest column number (see example 0).
-
If two purchases are less than 5 minutes apart, the machine does not perform a rotation to the most expensive column between the purchases. If two purchases are 5 or more minutes apart, the machine rotates to the most expensive column between the two purchases.
Constraints
-
prices will have between 1 and 50 elements, inclusive.
-
Each element of prices will have between 5 and 50 characters, is a single-space separated list of integers, and has no leading or trailing spaces.
-
Each element of prices will have the same number of integers in it.
-
Each element of prices will have at least 3 integers in it.
-
Each integer in prices will be between 1 and 10000, inclusive, and will not contain leading 0's.
-
purchases will have between 1 and 50 elements, inclusive.
-
Each element of purchases will be in the format "<shelf>,<column>:<time>" (angle brackets and quotes are for clarity only), where <shelf>, <column>, and <time> are all integers.
-
In each element of purchases, <shelf> will be between 0 and M - 1, inclusive, where M is the number of elements in prices.
-
In each element of purchases, <column> will be between 0 and N - 1, inclusive, where N is the number of integers in each element of prices.
-
In each element of purchases, <time> will be between 0 and 1000, inclusive.
-
In each element of purchases, <shelf>, <column>, and <time> will not contain extra leading 0's.
-
purchases will be sorted in strictly ascending order by <time>. This means that each purchase must occur later than all previous ones.
Examples
0)
????
{"100 100 100"}
{"0,0:0", "0,2:5", "0,1:10"}
Returns: 4
The vending machine has three columns, and only one row. Since all three items are the same price, they are all the most expensive, and therefore, the lowest numbered column is rotated to. Since the machine starts out at column 0, no rotation is performed before the first purchase. The starting configuration is (*'s denote the currently displayed column):
+-----+-----+-----+
| 100 | 100 | 100 |
+*****+-----+-----+
In the first purchase, the customer does not rotate the cylinder because the item he wants is already displayed. The configuration of the vending machine is now:
+-----+-----+-----+
| 0 | 100 | 100 |
+*****+-----+-----+
Since the next purchase is at least 5 minutes away, the machine performs a rotation to the most expensive column. Both column 1 and 2 are now 100 apiece, so it rotates to the smallest index of these, column 1. The fastest way there is to rotate forward 1 column, yielding 1 second of motor use:
+-----+-----+-----+
| 0 | 100 | 100 |
+-----+*****+-----+
The next customer purchases the item in column 2, which is 1 column away, so add 1 second to the motor use. Because another 5 minutes passes, the most expensive column is displayed, which is now column 1. Add 1 more second for the rotation. The configuration is now:
+-----+-----+-----+
| 0 | 100 | 0 |
+-----+*****+-----+
The final customer purchases from column 1, (which is already displayed), and the final most expensive column is rotated to. Since all columns are the same price again (0), column 0 is displayed. It takes 1 second to get back there, so add 1 more second.
1)
????
{"100 200 300 400 500 600"}
{"0,2:0", "0,3:5", "0,1:10", "0,4:15"}
Returns: 17
The most expensive column during this whole example is column 5. Since all purchases are at least 5 minutes apart, the most expensive column is rotated to each time.
Before the purchases start, add 1 second for rotating to column 5. The first purchase is 3 columns away, so add 3 seconds to get there, and 3 seconds to get back to column 5 The second purchase is 2 columns away, so add 4 seconds to get there and back. The third purchase is also 2 columns away, so add 4 more seconds. The final purchase is only one column away, so add 2 more seconds.
The final configuration is:
+-----+-----+-----+-----+-----+-----+
| 100 | 0 | 0 | 0 | 0 | 600 |
+-----+-----+-----+-----+-----+*****+
2)
????
{"100 200 300 400 500 600"}
{"0,2:0", "0,3:4", "0,1:8", "0,4:12"}
Returns: 11
This is the same as example 1, except now, the purchases are all less than 5 minutes apart.
3)
????
{"100 100 100"}
{"0,0:10", "0,0:11"}
Returns: -1
The second purchase is illegal since the item was already purchased
4)
????
{"100 200 300",
"600 500 400"}
{"0,0:0", "1,1:10", "1,2:20",
"0,1:21", "1,0:22", "0,2:35"}
Returns: 6
A two-row example. The configurations just before each purchase are:
purchase 1:
+-----+-----+-----+
| 100 | 200 | 300 |
+-----+-----+-----+
| 600 | 500 | 400 |
+*****+-----+-----+
purchase 2:
+-----+-----+-----+
| 0 | 200 | 300 |
+-----+-----+-----+
| 600 | 500 | 400 |
+-----+*****+-----+
purchase 3:
+-----+-----+-----+
| 0 | 200 | 300 |
+-----+-----+-----+
| 600 | 0 | 400 |
+-----+-----+*****+
purchase 4:
+-----+-----+-----+
| 0 | 200 | 300 |
+-----+-----+-----+
| 600 | 0 | 0 |
+-----+-----+*****+
purchase 5:
+-----+-----+-----+
| 0 | 0 | 300 |
+-----+-----+-----+
| 600 | 0 | 0 |
+-----+*****+-----+
purchase 6:
+-----+-----+-----+
| 0 | 0 | 300 |
+-----+-----+-----+
| 0 | 0 | 0 |
+-----+-----+*****+
final:
+-----+-----+-----+
| 0 | 0 | 0 |
+-----+-----+-----+
| 0 | 0 | 0 |
+*****+-----+-----+
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved
没有什么特别的算法,就是比较繁琐,不过写完调试通过后还是很有成就感的。调试花了很长时间,第一次用NetBeans集成的Junit进行测试,感觉不错。代码如下:
相关推荐
"Topcoder SRM 499 第一题详解" Topcoder SRM 499 的第一题是一道简单的 Addition Game 题目,旨在考察程序员对问题的理解和算法设计能力。本文将详细讲解该题目的知识点和解题思路。 题目分析 该题目中,Fox ...
TopCoder 练习 一个 Eclipse 项目,它将包含我用 Java 编写的 TopCoder 的所有实践问题。 包命名约定 ... 类型###.DIV# Eclipse 版本信息 版本:Luna Service Release 1 (4.4.1) 版本号:20140925-1800
在SRM144DIV2的250分问题中,题目要求编写一个方法,将从午夜开始的秒数转换为格式化的时分秒字符串。方法签名应为`String whatTime(int seconds)`。秒数参数的范围是0到86399秒(即0到24小时减1秒)。方法的实现...
【标题】"Topcoder入门"所指的,是全球知名在线编程竞赛平台Topcoder的初学者指南。这个资源集合可能是为了帮助对算法竞赛和软件开发感兴趣的人,特别是那些想要提升编程技能,尤其是ACM(国际大学生程序设计竞赛)...
【TOPCODER算法PPT1】是一份关于2007年TopCoder竞赛算法讲座的机密文件,它揭示了这个全球知名的编程竞赛平台的核心特点和价值。TopCoder社区是其核心,拥有遍布全球的会员,包括众多活跃的参赛者,涵盖了学生和专业...
Topcoder软件比赛注册方法和平台使用 Topcoder算法大赛客户端安装流程 Topcoder算法大赛客户端登陆及使用 Topcoder算法大赛注册流程 Topcoder图形比赛注册方法和平台使用
适合topcoder新手
2. **CodeProcessor.jar**:这是一个Java归档(JAR)文件,包含了topcoder arena中的代码处理逻辑。在比赛中,参赛者编写代码,CodeProcessor会负责编译、测试和评估这些代码,以确定其正确性和性能。 3. **File...
2. **TopCoder广东社区第一次竞赛华工赛区竞赛指南.doc**:这份文档可能针对特定的地区性竞赛,比如广东社区的首次比赛,并且可能包含华工(华南理工大学)赛区的具体信息,包括比赛规则、时间安排、评分标准和参赛...
在IT领域,TopCoder是一个知名的在线编程竞赛平台,它为程序员提供了一个展示技术才华、提升编程技能以及与全球开发者竞技的舞台。注册并参与TopCoder的比赛,不仅能提高自己的编程能力,还能有机会接触到实际项目...
TOPCODER算法ppt2 TOPCODER算法讲座ppt是一个关于TopCoder算法竞赛的讲座ppt,涵盖了TopCoder竞赛的概述、Component Based Methodology、组件目录、软件需求等内容。 在Component Based Methodology中,讲座强调了...
TopCoder比赛登录使用的客户端,需要配置Java环境
TopCoder是一个专注于在线程序设计竞赛的平台,它为程序设计爱好者提供了一个展示自己编码能力的舞台,同时也作为一个猎头公司,为业界发掘和选拔优秀的编程人才。在这个平台上,参赛者可以参加不同类型的竞赛,其中...
topcoder的在线答题系统
【TopCoder中文指导】是一份综合性的资源集合,旨在帮助用户了解并参与TopCoder平台的各种竞赛活动。TopCoder是一个全球知名的在线编程竞赛平台,它提供了多种类型的比赛,包括SRM(Single Round Matches)算法竞赛...
Topcoder的Java客户端,安装前确定已经安装了JRE
2. "point-250.cpp":这是一个C++源代码文件,命名中的“250”可能同样对应了另一个问题的分数。C++是编程竞赛中常用的编程语言之一,以其高效和灵活性而受青睐。这个文件很可能是参赛者为解决特定问题编写的代码。 ...
用于topcoder的第3方编辑器插件。