Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 8237 | Accepted: 2801 | Special Judge |
Description
John is the only priest in his town. September 1st is the John's busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of Love. This year N couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from time Si to time Ti. According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings. The i-th couple need Di minutes to finish this ceremony. Moreover, this ceremony must be either at the beginning or the ending of the wedding (i.e. it must be either from Si to Si + Di, or from Ti - Di to Ti). Could you tell John how to arrange his schedule so that he can present at every special ceremonies of the weddings.
Note that John can not be present at two weddings simultaneously.
Input
The first line contains a integer N ( 1 ≤ N ≤ 1000).
The next N lines contain the Si, Ti and Di. Si and Ti are in the format of hh:mm.
Output
The first line of output contains "YES" or "NO" indicating whether John can be present at every special ceremony. If it is "YES", output another N lines describing the staring time and finishing time of all the ceremonies.
Sample Input
2 08:00 09:00 30 08:15 09:00 20
Sample Output
YES 08:00 08:30 08:40 09:00
var a:array [1..3010,0..3010] of longint; v,f:array [1..3010] of boolean; low,dfn,st,belong,c:Array [1..3010] of longint; ff:array [1..3010,1..4] of longint; a1,a2,a3,a4,i,j,m,n,x,y,mm,nn,p,ii,s2,len,s1,d,jj,t,k:longint; ch:char; fff:boolean; s:string; procedure input1; begin readln(m); for i:=1 to m do begin readln(s); len:=length(s); jj:=0; x:=ord(s[1])*10+ord(s[2])-ord('0')*11; y:=ord(s[4])*10+ord(s[5])-ord('0')*11; mm:=ord(s[7])*10+ord(s[8])-ord('0')*11; nn:=ord(s[10])*10+ord(s[11])-ord('0')*11; for j:=13 to len do jj:=jj*10+ord(s[j])-ord('0'); s1:=x*60+y; s2:=mm*60+nn; ff[i,1]:=s1; ff[i,2]:=s1+jj; ff[i,3]:=s2-jj; ff[i,4]:=s2; end; end; procedure output1; begin for i:=1 to m do begin if belong[i]>belong[i+m] then begin a1:=ff[i,1] div 60; a2:=ff[i,1] mod 60; a3:=ff[i,2] div 60; a4:=ff[i,2] mod 60; writeln(a1 div 10,a1 mod 10,':',a2 div 10,a2 mod 10,' ',a3 div 10,a3 mod 10,':',a4 div 10,a4 mod 10); end else begin a1:=ff[i,3] div 60; a2:=ff[i,3] mod 60; a3:=ff[i,4] div 60; a4:=ff[i,4] mod 60; writeln(a1 div 10,a1 mod 10,':',a2 div 10,a2 mod 10,' ',a3 div 10,a3 mod 10,':',a4 div 10,a4 mod 10); end; end; end; function pp:longint; begin f[st[t]]:=false; dec(t); exit(st[t+1]); end; procedure add(x,y:longint); begin inc(c[x]); a[x,c[x]]:=y; end; procedure check1; var i:longint; begin for i:=1 to m do if belong[i]=belong[m+i] then begin fff:=false; exit; end; end; function min(x,y:longint):longint; begin if x>y then exit(y); exit(x); end; procedure check(x,y:longint); begin if not ((ff[x,2]<=ff[y,1]) or (ff[x,1]>=ff[y,2])) then begin add(y+m,x); add(x+m,y); end; if not ((ff[x,4]<=ff[y,3]) or (ff[x,3]>=ff[y,4])) then begin add(y,x+m); add(x,y+m); end; if not ((ff[x,3]>=ff[y,2]) or (ff[x,4]<=ff[y,1])) then begin add(x,y); add(y+m,m+x); end; if not ((ff[x,2]<=ff[y,3]) or (ff[x,1]>=ff[y,4])) then begin add(y,x); add(x+m,y+m); end; end; procedure tarjan(x:longint); var i,y:longint; begin inc(d); low[x]:=d; dfn[x]:=d; inc(t); st[t]:=x; f[x]:=true; for i:=1 to c[x] do begin if not v[a[x,i]] then begin v[a[x,i]]:=true; tarjan(a[x,i]); low[x]:=min (low[x],low[a[x,i]]); end else begin if f[a[x,i]] then low[x]:=min(low[x],dfn[a[x,i]]); end; end; if dfn[x]=low[x] then begin inc(p); belong[x]:=p; y:=x-1; while x<>y do begin y:=pp; belong[y]:=p; end; end; end; begin input1; for i:=1 to m do for j:=i+1 to m do check(i,j); for i:=1 to 2*m do if not v[i] then begin v[i]:=true; tarjan(i); end; fff:=true; check1; if fff then writeln('YES') else begin writeln('NO'); halt; end; output1; end.