This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;
const int maxn = 1e5 + 100;
inline int read(){
int x = 0;char c = getchar();
while(!isdigit(c))c = getchar();
while(isdigit(c))x = x * 10 + c - '0',c = getchar();
return x;
}
vector<int> G[16],vec[16];
int n,beg,end,cnt[16],pos[16],nxt[maxn][4];
clock_t begt;
inline int get(int x){
return pos[x] == vec[x].size() ? 0x7fffffff : vec[x][pos[x]];
}
inline void msort(int id){
if(get(nxt[id][1]) < get(nxt[id][0]))swap(nxt[id][1],nxt[id][0]);
if(get(nxt[id][2]) < get(nxt[id][0]))swap(nxt[id][2],nxt[id][0]);
if(get(nxt[id][2]) < get(nxt[id][1]))swap(nxt[id][1],nxt[id][2]);
}
inline void add(int u,int v){G[u].push_back(v);}
inline void init(){
add(1,3);add(1,4);add(1,8);
add(2,1);add(2,2);add(2,7);
add(3,3);add(3,4);add(3,8);
add(4,1);add(4,2);add(4,7);
add(5,3);add(5,4);add(5,8);
add(6,1);add(6,2);add(6,7);
}
int ans[maxn];
inline void dfs(int step,int lst){
if((clock() - begt) * 1000 / CLOCKS_PER_SEC >= 900){
puts("-1");
exit(0);
}
if(step == n){
for(int i = 1;i <= n;i++)printf("%d ",ans[i]);
puts("");
exit(0);
}
for(int i = 0;i < G[lst].size();i++)nxt[step][i] = G[lst][i];
msort(step);
for(int i = 0;i < G[lst].size();i++){
int v = nxt[step][i];
if(!cnt[v])continue;
ans[step + 1] = vec[v][pos[v]];
pos[v]++;
cnt[v]--;
dfs(step + 1,v);
pos[v]--;
cnt[v]++;
}
}
int main(){
// freopen("slagalica.in","r",stdin);
// freopen("slagalica.out","w",stdout);
begt = clock();
n = read();
for(int x,i = 1;i <= n;i++){
int a = read(),b = read();
cnt[a]++;
vec[a].push_back(b);
}
for(int i = 1;i <= 8;i++)sort(vec[i].begin(),vec[i].end());
init();
if(cnt[5])ans[1] = vec[5][0],dfs(1,5);
else if(cnt[6])ans[1] = vec[6][0],dfs(1,6);
puts("-1");
fclose(stdin);
fclose(stdout);
return 0;
}
Compilation message (stderr)
slagalica.cpp: In function 'int get(int)':
slagalica.cpp:18:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | return pos[x] == vec[x].size() ? 0x7fffffff : vec[x][pos[x]];
| ~~~~~~~^~~~~~~~~~~~~~~~
slagalica.cpp: In function 'void dfs(int, int)':
slagalica.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0;i < G[lst].size();i++)nxt[step][i] = G[lst][i];
| ~~^~~~~~~~~~~~~~~
slagalica.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i = 0;i < G[lst].size();i++){
| ~~^~~~~~~~~~~~~~~
slagalica.cpp: In function 'int main()':
slagalica.cpp:63:10: warning: unused variable 'x' [-Wunused-variable]
63 | for(int x,i = 1;i <= n;i++){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |