-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path161 - Traffic Lights.cpp
79 lines (56 loc) · 1.65 KB
/
161 - Traffic Lights.cpp
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
#include<bits/stdc++.h>
#define bug cout<<"bug";
#define pb(n) push_back(n)
#define lli long long int
#define fr(i,s,l) for(i=s;i<l;i++)
#define sf(n) scanf("%d",&n);
#include<stdlib.h>
using namespace std;
template<class T>
inline bool inRange(T ans,T st,T lst)
{
return (ans>=st && ans < lst);
}
int main()
{
int test;
while(1)
{
vector<int> str;
int num,i,j,k;
sf(num);
if(num==0) return 0;
while(1)
{
str.push_back(num);
sf(num);
if(num==0)
break;
}
int siz=str.size();
vector<int> strt(siz,0);
int ans=*min_element(str.begin(),str.end()),tmp; // taking the lowest time of any light changes to orange as ans
bool chk=true;
for(;ans<=18000 ; ans++)
{
chk=true;
for(i=0; i<siz; i++)
{
while(strt[i]+str[i]-5<=ans)
strt[i]+=2*str[i]; // loop until the ans is within the time green light lasts
if(!inRange(ans,strt[i],strt[i]+str[i])) // checking if ans is in the range of green cycle
{
ans=strt[i]-1; // if not then make the ans starting time of the green cycle
chk=false;
break;
}
}
if(chk)
break;
}
if(ans>18000)
printf("Signals fail to synchronise in 5 hours\n");
else
printf("%02d:%02d:%02d\n",ans/3600,(ans%3600)/60,ans%60);
}
}