My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;

namespace Hef.TopCoder.Srm145.Div2
{
public class VendingMachine
{
int[][] prices;
int colCount;

public int motorUse(string[] prices, string[] purchases)
{
this.prices = new int[prices.Length][];
for (int s = 0; s < prices.Length; s += 1)
{
string[] ps = prices[s].Split(' ');
this.prices[s] = new int[ps.Length];
for (int c = 0; c < ps.Length; c += 1)
{
this.prices[s][c] = Int32.Parse(ps[c]);
}
}

this.colCount = this.prices[0].Length;

int usage = 0;

// start at best column
usage += this.rotate(this.calcBestCol());

// process purchases
foreach (string p in purchases)
{
string[] args = p.Split(',', ':');
int sec = this.purchase(int.Parse(args[0]), int.Parse(args[1]), int.Parse(args[2]));

if (sec == -1)
return -1;

usage += sec;
}

// end at best column
usage += this.rotate(this.calcBestCol());

return usage;
}

int lastTime = 0;
int lastCol = 0;

int purchase(int shelf, int column, int atTime)
{
if (this.prices[shelf][column] == 0)
return -1;

int sec = 0;

if (atTime - this.lastTime >= 5)
sec += this.rotate(this.calcBestCol());

sec += this.rotate(column);

this.prices[shelf][column] = 0;
this.lastTime = atTime;

return sec;
}

int rotate(int column)
{
int maxCol = Math.Max(this.lastCol, column);
int minCol = Math.Min(this.lastCol, column);

int sec = Math.Min(maxCol - minCol, this.colCount - maxCol + minCol);

this.lastCol = column;

return sec;
}

int calcBestCol()
{
int bestCol = 0;
int bestPrice = 0;

for (int c = 0; c < this.colCount; c += 1)
{
int colPrice = 0;
for (int s = 0; s < this.prices.Length; s += 1)
colPrice += this.prices[s][c];

if (colPrice > bestPrice)
{
bestCol = c;
bestPrice = colPrice;
}
}

return bestCol;
}
}
}

Change log

r5 by jonathan.hefner on Jun 22, 2008   Diff
Sync
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 2232 bytes, 102 lines
Powered by Google Project Hosting