forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH.cpp
More file actions
88 lines (74 loc) · 2.64 KB
/
H.cpp
File metadata and controls
88 lines (74 loc) · 2.64 KB
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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
using namespace std;
double value[15][66][66];
void init() {
REP(h,12) REP(m,60) REP(s,60) {
int ss = m*60 + s;
double big = (h % 12) * (360 / 12) + (ss / 3600.0) * 30;
double small = (ss / 3600.0) * 360;
if (small < big) small += 360;
value[h][m][s] = small - big;
}
}
int main() {
ios :: sync_with_stdio(false);
init();
int ntest; cin >> ntest;
FOR(test,1,ntest) {
int angle; string desc; int starth;
cin >> angle >> desc >> starth;
int saveh, savem, saves;
int dir = (desc == "after" ? 1 : -1);
for(int turn = 0, h = starth; turn < 12; ++turn, h = (h + dir + 12) % 12)
REP(m,60) REP(s,60) {
if (desc[0] == 't' && h%12 == starth%12) {
if (m || s) continue;
}
if (h%12 == starth%12) {
if (m == 0 && s == 0) continue;
}
double cur = value[h%12][m][s];
int nexth = h, nextm = m, nexts = s + 1;
if (nexts == 60) {
++nextm;
nexts = 0;
if (nextm == 60) {
++nexth;
nextm = 0;
}
}
double next = value[nexth%12][nextm][nexts];
if (next < cur) next += 360;
double val = angle;
while (val < cur) val += 360;
if (val <= next + 1e-6) {
if (val - cur < next - val) {
saveh = h;
savem = m;
saves = s;
goto done;
}
else {
if (nexth%12 == starth%12 && nextm == 0 && nexts == 0) continue;
saveh = nexth;
savem = nextm;
saves = nexts;
goto done;
}
}
}
done:
saveh %= 12;
if (saveh == 0) saveh = 12;
printf("Case %d: %d:%02d:%02d\n", test, saveh, savem, saves);
}
return 0;
}