# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
159264 | 2019-10-21T17:36:01 Z | ppnxblstr | Alternating Current (BOI18_alternating) | C++14 | 61 ms | 6392 KB |
#include<bits/stdc++.h> using namespace std; int main() { int i,j,n,m,l,r,t=0,y,z,s=0,u,v,a=-1,b,tail,htail,cmm,full; scanf("%d %d\n",&n,&m); int reci[m]={},recf[m]={}; int ans[m]={}; int count[n]={}; int alert[n]={}; int st[n]={},en[n]={}; int pin[n]={}; int pinsort[m]={}; for(i=0;i<n;i++) { pin[i]--; } for(i=0;i<m;i++) { scanf("%d %d",&l,&r); st[l-1]++; en[r-1]++; reci[i]=l-1; if(l>r) {count[n-1]++;} else if(r==n) {count[n-1]++;} recf[i]=r-1; if(pin[r-1]==-1) {pin[r-1]=i;s++;} } for(i=0;i<n-1;i++) { if(i==0) { count[0]=count[n-1]+st[0]-en[n-1]; } else { count[i]=count[i-1]+st[i]-en[i-1]; } } for(i=0;i<n;i++) { if(count[i]<2) {printf("impossible");return 0;} if(count[i]==2) {alert[t]=i;t++;} } y=0; z=0; if(t!=0) { for(i=alert[t-1]+1;i<=n+alert[t-1];i++) { j=i%n; z+=st[j]; if(z>=2) {break;} if(j==alert[y]) { y++;z=0; } } } int hi[s]={}; j=0; i=0; while(j<s) { if(pin[i]!=-1) { hi[j]=pin[i]; j++; } i++; } int gi[s]={}; int ad[s]={}; int rehi[n]={}; for(i=0;i<s;i++) { rehi[recf[hi[i]]]=i; } gi[0]=0; for(i=1;i<s;i++) { gi[i]=gi[i-1]+en[recf[hi[i-1]]]; } for(i=0;i<m;i++) { u=rehi[recf[i]]; v=gi[u]+ad[u]; pinsort[v]=i; ad[u]++; } if(t==0) { cmm=n-1; int mst=n,mnd=n; for(i=0;i<m;i++) { if(recf[i]==n-1||recf[i]<reci[i]) { if(reci[i]<mst) { b=a; a=i; mnd=mst; mst=recf[i]; } else if(reci[i]<mnd) { b=i; mnd=recf[i]; } } } } else if(y==t) { cmm=alert[0]; for(i=0;i<m;i++) { if(reci[i]<=alert[0]&&recf[i]>=alert[0]||reci[i]>recf[i]&&(reci[i]<=alert[0]||recf[i]>=alert[0])) { if(a==-1) { a=i; } else { b=i; } } } for(tail=t-1;tail>0;tail--) { if((alert[tail]<reci[a]||reci[a]<=alert[0])&&(alert[tail]<reci[b]||reci[b]<=alert[0])) { tail++; break; } } if(alert[tail]>=reci[a]&&reci[a]>alert[0]) { htail=1; } else { htail=2; } } else { cmm=alert[y]; for(i=0;i<m;i++) { if(reci[i]<=alert[y]&&recf[i]>=alert[y]||reci[i]>recf[i]&&(reci[i]<=alert[y]||recf[i]>=alert[y])) { if(a==-1) { a=i; } else { b=i; } } } } ans[a]=1; ans[b]=2; int in2,fi2,in1,fi1,di2,di1,dic2,dic1,i1,i2,ff1,ff2; in2=reci[b]; in1=reci[a]; fi2=recf[b]; fi1=recf[a]; di2=(fi2-in2+n+1)%n; di1=(fi1-in1+n+1)%n; dic2=di2; dic1=di1; i1=0; i2=0; if(di1==0) { for(i=0;i<m;i++) { printf("%d",ans[i]%2); } return 0; } if(di2==0) { for(i=0;i<m;i++) { printf("%d",ans[i]/2); } return 0; } while(true) { if((fi2-cmm+n)%n>=(fi1-cmm+n)%n) { i1=(i1+1)%m; ff1=(fi1+1)%n; if(ans[pinsort[i1]]!=0) { continue; } if(ff1>=reci[pinsort[i1]]&&ff1<=recf[pinsort[i1]]||reci[pinsort[i1]]>recf[pinsort[i1]]&&(reci[pinsort[i1]]<=ff1||recf[pinsort[i1]]>=ff1)) { ans[pinsort[i1]]=1; fi1=recf[pinsort[i1]]; di1=(fi1-in1+n+1)%n; if(di1<=dic1) { full=1; break; } dic1=di1; } } else { i2=(i2+1)%m; ff2=(fi2+1)%n; if(ans[pinsort[i2]]!=0) { continue; } if(ff2>=reci[pinsort[i2]]&&ff2<=recf[pinsort[i2]]||reci[pinsort[i2]]>recf[pinsort[i2]]&&(reci[pinsort[i2]]<=ff2||recf[pinsort[i2]]>=ff2)) { ans[pinsort[i2]]=2; fi2=recf[pinsort[i2]]; di2=(fi2-in2+n+1)%n; if(di2<=dic2) { full=2; break; } dic2=di2; } } } if(t!=0&&y==t) { int lch1=-1,lch2; for(i=0;i<m;i++) { if(reci[i]<=alert[tail]&&recf[i]>=alert[tail]||reci[i]>recf[i]&&(reci[i]<=alert[tail]||recf[i]>=alert[tail])) { if(lch1!=-1) { lch2=i; break; } lch1=i; } } if(ans[lch1]==ans[lch2]) { printf("impossible"); } } if(full==1) { for(i=0;i<m;i++) { printf("%d",ans[i]%2); } return 0; } if(full==2) { for(i=0;i<m;i++) { printf("%d",ans[i]/2); } return 0; } }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 256 KB | Output is correct |
3 | Correct | 2 ms | 256 KB | Output is correct |
4 | Correct | 2 ms | 128 KB | Output is correct |
5 | Correct | 2 ms | 256 KB | Output is correct |
6 | Incorrect | 2 ms | 256 KB | no wires in direction 0 between segments 1 and 2 |
7 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 256 KB | Output is correct |
3 | Correct | 2 ms | 256 KB | Output is correct |
4 | Correct | 2 ms | 128 KB | Output is correct |
5 | Correct | 2 ms | 256 KB | Output is correct |
6 | Incorrect | 2 ms | 256 KB | no wires in direction 0 between segments 1 and 2 |
7 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 256 KB | Output is correct |
3 | Correct | 2 ms | 256 KB | Output is correct |
4 | Correct | 2 ms | 128 KB | Output is correct |
5 | Correct | 2 ms | 256 KB | Output is correct |
6 | Incorrect | 2 ms | 256 KB | no wires in direction 0 between segments 1 and 2 |
7 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 33 ms | 5140 KB | Output is correct |
2 | Correct | 5 ms | 2680 KB | Output is correct |
3 | Correct | 16 ms | 3576 KB | Output is correct |
4 | Correct | 21 ms | 4600 KB | Output is correct |
5 | Correct | 54 ms | 6264 KB | Output is correct |
6 | Correct | 40 ms | 5112 KB | Output is correct |
7 | Correct | 55 ms | 6140 KB | Output is correct |
8 | Correct | 7 ms | 2684 KB | Output is correct |
9 | Correct | 5 ms | 2552 KB | Output is correct |
10 | Correct | 55 ms | 6392 KB | Output is correct |
11 | Correct | 41 ms | 5496 KB | Output is correct |
12 | Correct | 45 ms | 6252 KB | Output is correct |
13 | Correct | 4 ms | 2296 KB | Output is correct |
14 | Correct | 4 ms | 2296 KB | Output is correct |
15 | Correct | 49 ms | 6140 KB | Output is correct |
16 | Correct | 16 ms | 3576 KB | Output is correct |
17 | Correct | 61 ms | 6140 KB | Output is correct |
18 | Correct | 36 ms | 3192 KB | Output is correct |
19 | Correct | 6 ms | 2424 KB | Output is correct |
20 | Correct | 38 ms | 4856 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 256 KB | Output is correct |
3 | Correct | 2 ms | 256 KB | Output is correct |
4 | Correct | 2 ms | 128 KB | Output is correct |
5 | Correct | 2 ms | 256 KB | Output is correct |
6 | Incorrect | 2 ms | 256 KB | no wires in direction 0 between segments 1 and 2 |
7 | Halted | 0 ms | 0 KB | - |