Test instructions
There are n students in class (N<=10). Each student has a "awake-sleep" cycle. One of the I students wakes up AI minutes later sleeps
Bi minutes and then repeats (1<=ai,bi<=5), initially when the first student is in the CI minute of his cycle. Each student in the pro-
Before going to bed, you will see if the number of people in the class is more stringent than the sober number, only when the condition is met, or if you persist in listening to AI
Check this condition again after a minute. Ask after how long after the whole class awake.
Ideas:
Record the time that you want to change with the priority queue and the student number that corresponds to the state you want to change, sorted from small to large at the time you want to change.
CNT records the number of people currently sleeping.
Note here: Because before bedtime to judge, but may exist in the queue at the same time x students sleep y classmates awake,
If you first deal with the waking classmate, and then the CNT directly modified, then the sleep of the people will be reduced, there may be a
Can sleep, because first deal with the awake person, so this judgment is inaccurate, because the judgment of the number of people should be this a second did not do
Number of persons at any time of processing. Processing should be done at the same time, so set a local variable sum to record the number of changes in this second.
The sum is added to the CNT at the time of the next change.
And how to judge that there is no "all awake moment"?
The intuitive feeling is that the number of people sleeping has not diminished.
I recorded the number of times each update was sum==0 and automatically jumped out of the loop 10,000 times. It seems to be less rigorous.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cstdlib>6#include <cmath>7#include <cctype>8#include <vector>9#include <queue>Ten#include <map> One#include <Set> A #defineEPS 10e-6 - - using namespacestd; the -typedefLong Longll; - - inta[ A],b[ A],c[ A]; + BOOLsleep[ A]; -priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >Q; + A intMain () at { - intn,cas=1; - while(~SCANF ("%d", &n) &&N) - { - intCNT =0;//number of people sleeping -memset (Sleep,0,sizeof(Sleep)); in while(!q.empty ()) - Q.pop (); to intAns =-1; + - for(intI=0; i<n;i++) the { *scanf"%d%d%d",&a[i],&b[i],&c[i]); $ //C[i] = c[i]% (A[i]+b[i]);Panax Notoginseng if(c[i]>A[i]) - { thecnt++; +sleep[i]=true; AQ.push (Make_pair (a[i]+b[i]-c[i]+2, i)); the } + ElseQ.push (Make_pair (a[i]-c[i]+2, i)); - } $ intPre =0, sum =0; $ intFun =0; - while(!q.empty ()) - { thepair<int,int> now =q.top (); - Q.pop ();Wuyi intID =Now.second; the intt =Now.first; - Wu if(t>pre) - { AboutCNT + =sum; $ if(sum==0) fun++; - if(fun>=10000) Break; -sum =0; - if(cnt==0) A { +Ans =Pre; the Break; - } $Pre =T; the } the the if(Sleep[id]) the { -Sleep[id] =false; insum--; theQ.push (Make_pair (t+a[id],id)); the } About Else the { the if(cnt<=n-CNT) the { +Q.push (Make_pair (t+a[id],id)); - Continue; the }BayiSleep[id] =true; thesum++; theQ.push (Make_pair (t+b[id],id)); - } - } theprintf"Case %d:%d\n", cas++, ans); the } the return 0; the}View Code
Uva 12108 extraordinarily tired Students