Ultraviolet A 467-Synching Signals (number theory)

Source: Internet
Author: User

Ultraviolet A 467-Synching Signals

Question Link

Given several traffic lights, each traffic light time indicates a red light in time seconds, a green light in time-5 seconds, and a yellow light in 5 seconds.
After all the lights turn green, after a change, all the time required for the green light

Idea: as long as the time is counted as one hour, that is, 3600 seconds, the brute force is passed. Each time, the minimum time that can change the light is added, and the color status of each lamp is recorded, until all turns green

Code:

# Include
 
  
# Include
  
   
# Include using namespace std; char str [105]; int ti [15], s [15], r [15], n; int cas = 0; void solve () {int k = ti [0]; // s = 0 green, s = 1 yellow, s = 2 red memset (s, 0, sizeof (s )); memset (r, 0, sizeof (r); for (int I = 1; I <n; I ++) k = min (k, ti [I]); k-= 5; int t = k; for (int I = 0; I <n; I ++) {r [I] = (ti [I]-5) -k; if (r [I] = 0) {r [I] = 5; s [I] = 1;} else s [I] = 0 ;} while (t <= 3600) {k = r [0]; int sum = 0; (; Sum <n; sum ++) if (s [sum]) break; if (sum = n) break; for (int I = 1; I <n; I ++) k = min (k, r [I]); for (int I = 0; I <n; I ++) {r [I]-= k; if (r [I]) continue; if (s [I] = 0) {s [I] = 1; r [I] = 5 ;} else if (s [I] = 1) {s [I] = 2; r [I] = ti [I];} else {s [I] = 0; r [I] = ti [I]-5 ;}t + = k ;}if (t> 3600) printf ("Set % d is unable to synch after one hour. \ n ", ++ cas); else {int minu = t/60; int se C = t % 60; printf ("Set % d synchs again at % d minute (s) and % d second (s) after all turning green. \ n ", ++ cas, minu, sec) ;}int main () {while (gets (str )! = NULL) {int len = strlen (str); n = 0; str [len ++] = ''; int num = 0; for (int I = 0; I <len; I ++) {if (str [I]> = '0' & str [I] <= '9 ') {num = num * 10 + str [I]-'0'; continue;} ti [n ++] = num; num = 0;} solve ();} return 0 ;}
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.