# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
258198 | 2020-08-05T14:14:21 Z | sonvip9 | Slagalica (COCI19_slagalica2) | C++14 | 166 ms | 2552 KB |
#include<bits/stdc++.h> #define fi first #define se second #define pb push_back #define ll long long #define ii pair <int,int> #define pl pair <ll,ll> #define fr(i,x,y) for (int i=x;i<=y;i++) #define ft(i,x,y) for (int i=y;i>=x;i--) #define N 100005 using namespace std ; int d[8][8],dd[12],ans[N],cnt[9]; struct data{ int tpe; int w ; }; int n,x[12]; data puz[N]; vector <int> ans_sub2[9]; void inp() { ios_base::sync_with_stdio(false) ; cin.tie(0); cout.tie(0); //freopen("Slagalica.inp","r",stdin) ; //freopen("Slagalica.out","w",stdout); cin>> n; fr(i,1,n) { cin>>puz[i].tpe>>puz[i].w; } d[1][3] = 1,d[1][4] = 1 ,d[1][8] = 1 ; d[2][1] = 1,d[2][7] = 1,d[2][2] = 1; d[3][4] = 1,d[3][8] = 1,d[3][3] = 1; d[4][1] = 1,d[4][2] = 1 ,d[4][7] = 1 ; d[5][3] = 1,d[5][4] = 1 ,d[5][8] = 1 ; d[6][1] = 1,d[6][2] = 1 ,d[6][7] = 1 ; ans[1] = INT_MAX ; fr(i,1,n) { cnt[puz[i].tpe]++; ans_sub2[puz[i].tpe].push_back(puz[i].w); } fr(i,1,8) sort(ans_sub2[i].begin(),ans_sub2[i].end()); } void show(){ if (puz[x[1]].tpe!=5 and puz[x[1]].tpe!=6) return ; data g[12]; g[1] = puz[x[1]]; int dem_show = 1 ; fr(i,1,n-1) { if (d[puz[x[i]].tpe][puz[x[i+1]].tpe]==1){ g[++dem_show] = puz[x[i+1]]; } if (puz[x[i+1]].tpe==7 or puz[x[i+1]].tpe==8) break; } if (dem_show!=n) return ; if (g[dem_show].tpe!= 7 and g[dem_show].tpe!=8) return ; fr(i,1,n){ if (g[i].w<=ans[i]) { if (g[i].w==ans[i]) continue ; else { fr(j,1,n) ans[j] = g[j].w ; break ; } } else break ; } } void hoanvi(int u){ if (u==n+1){ show(); return ; } fr(i,1,n){ if (dd[i]==0){ x[u] = i ; dd[i] = 1 ; hoanvi(u+1) ; dd[i] = 0; } } } void sub1() { hoanvi(1); if (ans[1]==INT_MAX) cout<<"-1"; else fr(i,1,n) cout<<ans[i]<<" "; } void sub2(){ fr(i,1,8) dd[i] = INT_MAX; if (cnt[5]*cnt[6]!=0) { cout<<"-1" ; return ; } if (cnt[6]==1){ if (ans_sub2[1].size()!=ans_sub2[4].size() or cnt[6]+cnt[7]!=2 or cnt[1]==0){ cout<<"-1"; } else { cout<<ans_sub2[6][0]<<" "; fr(i,0,ans_sub2[4].size()-1) cout<<ans_sub2[1][i]<<" "<<ans_sub2[4][i]<<" "; cout<<ans_sub2[7][0]<<" "; } return ; } if (cnt[5]==1){ if (ans_sub2[1].size()!=ans_sub2[4].size() or cnt[5]+cnt[8]!=2 or cnt[1]==0){ if (cnt[7]==1 and (int)ans_sub2[4].size()-(int)ans_sub2[1].size()==1){ cout<<ans_sub2[5][0]<<" "; fr(i,0,ans_sub2[1].size()-1) cout<<ans_sub2[4][i]<<" "<<ans_sub2[1][i]<<" "; cout<<ans_sub2[4][ans_sub2[4].size()-1]<<" "<<ans_sub2[7][0]; return ; } cout<<"-1"; } else { cout<<ans_sub2[5][0]<<" "; fr(i,0,ans_sub2[4].size()-1) cout<<ans_sub2[4][i]<<" "<<ans_sub2[1][i]<<" "; cout<<ans_sub2[8][0]<<" "; } return ; } cout<<"-1"; } void sub3(){ if (cnt[5]*cnt[6]!=0 or cnt[7]*cnt[8]!=0){ cout<<"-1"; return ; } cout<<cnt[7]<<" "<<cnt[5]<<" "<<cnt[6]<<" "<<cnt[1]<<" "<<cnt[4]<<" "<<cnt[2]<<" "<<cnt[3]<<" "<<cnt[8]<<endl; if (cnt[1]==0 and cnt[4]==0){ if (cnt[6]==1){ if (cnt[7]==1 and n-cnt[6]-cnt[7]==cnt[2]) { cout<<ans_sub2[6][0]<<" "; for (int v:ans_sub2[2]) cout<<v<<" "; cout<<ans_sub2[7][0]<<" "; } else cout<<"-1"; } if (cnt[5]==1){ if (cnt[8]==1 and n-cnt[5]-cnt[8]==cnt[3]) { cout<<ans_sub2[5][0]<<" "; for (int v:ans_sub2[3]) cout<<v<<" "; cout<<ans_sub2[8][0]<<" "; } else cout<<"-1"; } return ; } if (cnt[1]==0 and cnt[4]!=0){ if (cnt[6]==1){ cout<<"-1"; } if (cnt[5]==1){ if (cnt[7]==1 and n-cnt[7]-cnt[5]-cnt[4]==cnt[2]) { cout<<ans_sub2[5][0]<<" "<<ans_sub2[4][0]<<" "; for (int v:ans_sub2[2]) cout<<v<<" "; cout<<ans_sub2[7][0]<<" "; } else if (cnt[7]==1 and n-cnt[2]-cnt[5]-cnt[7]-cnt[4]==cnt[3]){ cout<<ans_sub2[5][0]<<" "; for (int v:ans_sub2[3]) cout<<v<<" "; cout<<ans_sub2[4][0]<<" "; for (int v:ans_sub2[2]) cout<<v<<" "; cout<<ans_sub2[7][0]<<" "; } else cout<<"-1"; } return ; } if (cnt[4]==0 and cnt[1]!=0){ if (cnt[5]==1){ cout<<"-1"; } if (cnt[6]==1){ if (cnt[8]==1 and n-cnt[8]-cnt[6]-cnt[1]==cnt[3]) { cout<<ans_sub2[6][0]<<" "<<ans_sub2[1][0]<<" "; for (int v:ans_sub2[3]) cout<<v<<" "; cout<<ans_sub2[8][0]<<" "; } else if (cnt[8]==1 and n-cnt[6]-cnt[2]-cnt[1]-cnt[8]==cnt[3]){ cout<<ans_sub2[6][0]<<" "; for (int v:ans_sub2[2]) cout<<v<<" "; cout<<ans_sub2[1][0]<<" "; for (int v:ans_sub2[3]) cout<<v<<" "; cout<<ans_sub2[8][0]<<" "; } else cout<<"-1"; } return ; } if (cnt[4]==1 and cnt[1]==1){ if (cnt[5]==1){ if (n-cnt[5]-cnt[4]-cnt[1]-cnt[8]==cnt[3]){ cout<<ans_sub2[5][0]<<" "<<ans_sub2[4][0]<<" "<<ans_sub2[1][0]<<" "; for (int v:ans_sub2[3]) cout<<v<<" "; cout<<ans_sub2[8][0]<<" "; } else cout<<"-1"; } if (cnt[6]==1){ if (n-cnt[6]-cnt[4]-cnt[1]-cnt[7]==cnt[2]){ cout<<ans_sub2[6][0]<<" "<<ans_sub2[1][0]<<" "<<ans_sub2[4][0]<<" "; for (int v:ans_sub2[2]) cout<<v<<" "; cout<<ans_sub2[7][0]<<" "; } else cout<<"-1"; } } } int main() { inp(); if (n<=10) sub1(); else{ if (cnt[1]<=1 and cnt[4]<=1) sub3(); else sub2(); } }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 384 KB | Output is correct |
2 | Correct | 0 ms | 384 KB | Output is correct |
3 | Correct | 0 ms | 384 KB | Output is correct |
4 | Correct | 1 ms | 384 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 166 ms | 396 KB | Output is correct |
2 | Correct | 155 ms | 512 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 33 ms | 2420 KB | Output is correct |
2 | Correct | 28 ms | 1676 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 33 ms | 2356 KB | Output is correct |
2 | Correct | 26 ms | 1536 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 32 ms | 1752 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 27 ms | 1588 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 40 ms | 2328 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 34 ms | 2292 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 26 ms | 1664 KB | Output is correct |
2 | Incorrect | 34 ms | 2552 KB | Output isn't correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 35 ms | 2404 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 31 ms | 2024 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 29 ms | 1664 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 33 ms | 2048 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 30 ms | 2096 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |