2013-05-29 15:16:22Morris

[UVA] 467 - Synching Signals


 Synching Signals 

On your way to work wach morning you travel down a main traffic artery regulated by a number of traffic signals. On some mornings, you have noticed that all of the lights up ahead simultaneously turn green in your direction. But on other mornings, it seems that a random combination of red, yellow, and green is facing you.

After recently observing all of the lights ahead of you turn green simultaneously, you began to wonder how long after any one of them turns to yellow it would take before they all would be displaying green in your direction again. Write a program to figure out how long it will take such a set of traffic signals to all be displaying green again, given the cycle times for each traffic signal in the set.

Input

The input file will consist of an unknown number of traffic signal datasets. Each signal dataset will consist of one line of integers (separated by spaces) giving the total cycle time (in seconds) for each signal. The cycle time is the total time that a signal will stay green and yellow in one direction and red in the opposite direction. In this problem, you may assume that a green signal will turn yellow for the last 5 seconds of its cycle. If one set of signals was given as:

displaymath27

your program would need to recognize that there are three signals in the set, that the first signal lasts 30 seconds, that the second signal cycles every 25 seconds, and that the thirs signal takes 35 seconds to start a new cycle. Specifically, the first signal will be red for 30 seconds in one direction, then green for 25 seconds, then yellow for 5.

Each set of signals will involve at least two and as many as 10 signals. Each signal will have a minimum cycle time of 10 seconds and a maximum cycle time of 90 seconds.

Output

Your output will consist of a summary line for each set of signals. Your program should assign an ID number to each set of signals, beginning with set 1. The output line will begin with the signal set ID number and state the number of minutes ( tex2html_wrap_inline29 ) and seconds (< 60) it will take from when all of the signals simultaneously turn green initially to the first time they will all be showing green again in your direction after any of them has turned yellow. Note that this time may or may not be a time when all of the signals in the set simultaneously change back to green -- for this problem you need only indicate how long it will be before all signals in the set are once again simultaneously showing green in your direction after any of them has turned to yellow, even if this condition will only exist for a second or a very few seconds.

If the signals will never simultaneously display green in your direction again within an hour, you should print a message that states that the signals in the set are not able to synchronize after one hour (note that an output of 60 minutes and 0 seconds should, however, be considered a successful synchronization).

Look at the sample output below for the exact format.

Sample Input

30 25 35
25 25 25 25 25
15 30
20 21 30 23 29 25 27 22
19 20

Sample Output

Set 1 synchs again at 5 minute(s) and 0 second(s) after all turning green.
Set 2 synchs again at 0 minute(s) and 50 second(s) after all turning green.
Set 3 synchs again at 1 minute(s) and 0 second(s) after all turning green.
Set 4 is unable to synch after one hour.
Set 5 synchs again at 0 minute(s) and 40 second(s) after all turning green.

題目描述:
一堆紅綠燈的週期時間,現在全部號誌同時轉綠,問距離下次都是綠燈(並不用都剛切入綠燈)
的時間還有多久?而週期是這麼計算的,當一個號誌週期 x,

(x)秒紅燈-(x-5)秒綠燈-(5)秒黃燈-
(x)秒紅燈-(x-5)秒綠燈-(5)秒黃燈-...

而當距離開始時間,其中一盞號誌轉黃燈之後,才能開始計算下次都是綠燈的時間點。

模擬就是了,看仔細好好看題目真是罪過,一直 Wrong Answer.



#include <stdio.h>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;

int main() {
string line;
int cases = 0;
while(getline(cin, line)) {
int v[7200] = {}, x;
int i, j, k, cnt = 0;
stringstream sin(line);
int mx = 0xfffffff, time = 0xfffffff;
while(sin >> x) {
mx = min(mx, x);
int base = -x;
cnt++;
while(base <= 3600) {
base += x; // red
for(i = base+1, j = 0; j < x-5; i++, j++)
v[i]++;
base += x;
}
}
for(i = mx; i <= 3601; i++) {
if(v[i] == cnt) {
time = i;
break;
}
}
time--;
printf("Set %d ", ++cases);
if(time <= 3600) {
printf("synchs again at %d minute(s) and %d second(s) after all turning green.\n", time/60, time%60);
} else {
puts("is unable to synch after one hour.");
}
}
return 0;
}