-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblemC.cs
238 lines (199 loc) · 6.09 KB
/
ProblemC.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.IO;
/// <summary>
/// @author: jaracoder
/// @date: 09/04/2017
///
/// Problem C. Bathroom Stalls
/// Qualification Round 2017 - Google Code Jam
/// </summary>
///
namespace GoogleCodeJam17
{
public class ProblemC
{
const byte NUMBER_GUARDS = 2;
static char[] bathroom;
static double toilettes;
static double persons;
static int numberTurn;
static int totalSpacesToLeft;
static int totalSpacesToRight;
static string inFile = "C-small-1-attempt1.in";
static string outFile = "C-small-1-attempt0.out";
static string[] inputLines = null;
static string[] outputLines = null;
static int totalCases;
static void Main()
{
try
{
inputLines = GetLinesFromFile();
totalCases = Int32.Parse(inputLines[0]);
outputLines = new string[totalCases];
for (int i = 1; i < inputLines.Length; i++)
{
toilettes = Convert.ToDouble(inputLines[i].Split(' ')[0].Replace(",", "."));
persons = Convert.ToDouble(inputLines[i].Split(' ')[1].Replace(",", "."));
InitBathroom((int)toilettes + NUMBER_GUARDS);
if (toilettes > persons)
{
do
{
numberTurn = GetTurnForToilette();
bathroom[numberTurn] = '0';
persons--;
} while (persons > 0);
totalSpacesToLeft = GetTotalSpaces(numberTurn - 1, true);
totalSpacesToRight = GetTotalSpaces(numberTurn + 1, false);
if (totalSpacesToLeft > totalSpacesToRight)
{
outputLines[i - 1] = "Case #" + i + ": " +
totalSpacesToLeft + " " + totalSpacesToRight;
}
else
{
outputLines[i - 1] = "Case #" + i + ": " +
totalSpacesToRight + " " + totalSpacesToLeft;
}
}
else
{
outputLines[i - 1] = "Case #" + i + ": 0 0";
}
}
SaveLinesOnFile(outputLines);
}
catch { }
}
/// <summary>
/// Initialize the bathroom array
/// </summary>
static void InitBathroom(int size)
{
try
{
bathroom = new char[size];
for (int i = 0; i < bathroom.Length; i++)
{
bathroom[i] = '.';
}
bathroom[0] = '0';
bathroom[bathroom.Length - 1] = '0';
}
catch
{
}
}
/// <summary>
/// Gets the next turn for toilette by following the rules.
/// </summary>
static int GetTurnForToilette()
{
int total = 0, count = 0;
int median = 0;
// check the central position
if ((bathroom.Length - 2) % 2 == 0)
{
median = (bathroom.Length - 2) / 2;
}
else
{
median = ((bathroom.Length - 2) / 2) + 1;
}
if (bathroom[median] == '.')
{
return median;
}
// Get the next position number
for (int i = 0; i < bathroom.Length; i++)
{
if (bathroom[i] == '.')
{
count++;
}
else
{
if (count > total)
{
total = count;
count = 0;
if (total % 2 == 0)
{
median = i - (total / 2);
}
else
{
median = i - (total / 2) - 1;
}
}
}
}
return median;
}
/// <summary>
/// It obtains the number of free spaces to the left and right respectively.
/// </summary>
static int GetTotalSpaces(int from, bool leftDirection)
{
int total = 0;
char symbol = ' ';
try
{
if (leftDirection)
{
symbol = bathroom[from];
while (symbol == '.')
{
if (symbol == '.')
total++;
from--;
symbol = bathroom[from];
}
}
else
{
symbol = bathroom[from];
while (symbol == '.')
{
if (symbol == '.')
total++;
from++;
symbol = bathroom[from];
}
}
}
catch
{
}
return total;
}
/// <summary>
/// Gets all lines of a text file.
/// </summary>
static string[] GetLinesFromFile()
{
string[] lines;
try
{
lines = File.ReadAllLines(inFile);
}
catch
{
lines = null;
}
return lines;
}
/// <summary>
/// Gets all lines of a text file.
/// </summary>
static void SaveLinesOnFile(string[] lines)
{
try
{
File.WriteAllLines(outFile, lines);
}
catch { }
}
}
}