#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define MEM(x) memset(x, 0, sizeof(x))
#define ALL(x) begin(x), end(x)
#define REP(i,n,...) for( int i = 0, ##__VA_ARGS__; i < n; ++i )
#define FOR(i,a,b,...) for( int i = (a), ##__VA_ARGS__; i <=(b); ++i)
#define RFOR(i,a,b,...) for( int i = (a), ##__VA_ARGS__; i >=(b); --i)
#define EB emplace_back
#define EP emplace_back
#define mkp make_pair
#define SZ(x) int(x.size())
#ifdef DEBUG
template<typename T> void _DO(T&&x) {cerr << x<< '\n';}
template<typename A, typename ...B> void _DO(A&&a, B&&...b) {
cerr <<a << ',';
_DO(b...);
}
#define de(...) do{\
fprintf(stderr, "%s-%d (%s):", __func__, __LINE__, #__VA_ARGS__);\
_DO(__VA_ARGS__);\
}while(0);
#else
#define de(...)
#endif
typedef long long ll;
typedef pair<int,int> pii;
template<typename T>
using V = vector<T>;
inline void solve() {
int N; cin >> N;
set< pair<int,int> > st[ 2 ]; // val, jump to
set< pair<int,int> > slf[ 2 ];
pii sta = mkp(-1, -1), ed =mkp(-1,-1);
int stcnt, edcnt;
stcnt = edcnt = 0;
int ct[ 2 ] = {0, 0};
REP(i,N) {
int x, val;
cin >> x >> val;
if( x == 1 )
st[1] .insert( mkp( val, 0 ) ), ct[ 0 ]++;
if( x == 2 )
st[1] .insert( mkp( val, 1 ) ), slf[ 1 ].insert( mkp( val, 1 ));
if( x == 4 )
st[0] .insert( mkp( val, 1 ) ), ct[ 1 ]++;
if( x == 3 )
st[0] .insert( mkp( val, 0 ) ), slf[ 0 ].insert( mkp(val, 0 ));;
if( x == 5 )
stcnt++, sta=mkp( val, 0 );
if( x == 6 )
stcnt++, sta=mkp( val,1 );
if( x == 7 )
edcnt++, ed=mkp( val ,1 );
if( x == 8 )
edcnt++, ed=mkp( val, 0 );
}
if( stcnt != 1 || edcnt != 1 ) {
cout << -1 << '\n';
return;
}
V<int> ans;
ans.push_back( sta.F );
int np = sta.S;
while( !st[ np ].empty() ) {
auto p = *st[ np ].begin();
de( "start", np, ans.back(), p.F, p.S );
if( np != p.S ) {
if( ct[ np ] == 0 && ct[ p.S ] == 1 && !slf[ np ].empty() ) {
auto newp = *slf[ np ].begin();
//de("ENDD", newp.F, newp.S );
ans.push_back( newp.F );
st[ np ].erase( newp );
slf[ np ].erase( newp );
continue;
}
else {
ct[ p.S ]--;
}
}
ans.push_back( p.F );
st[ np ].erase( st[ np ].begin() );
if( np == p.S ) slf[ np ].erase( p );
np = p.S;
}
if( np != ed.S ) {
de("NP != ed.s");
cout << -1 << '\n';
return;
}
ans.push_back( ed.F );
if( ans.size() != N ) {
cout << -1 << '\n';
return;
}
for( auto &i : ans ) {
cout << i << ' ';
}
cout << '\n';;
}
int main () {
int T = 1;
#ifdef Local
cin >> T;
#endif
while(T--) solve();
}
Compilation message
slagalica.cpp: In function 'void solve()':
slagalica.cpp:99:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
99 | if( ans.size() != N ) {
| ~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
5640 KB |
Output is correct |
2 |
Correct |
96 ms |
5304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
5416 KB |
Output is correct |
2 |
Correct |
92 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
9964 KB |
Output is correct |
2 |
Correct |
133 ms |
10120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
8616 KB |
Output is correct |
2 |
Correct |
125 ms |
9180 KB |
Output is correct |
3 |
Correct |
151 ms |
10928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
9704 KB |
Output is correct |
2 |
Correct |
126 ms |
9412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
9448 KB |
Output is correct |
2 |
Correct |
116 ms |
9220 KB |
Output is correct |
3 |
Correct |
123 ms |
10480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
6572 KB |
Output is correct |
2 |
Correct |
99 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
8292 KB |
Output is correct |
2 |
Correct |
100 ms |
6152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
6592 KB |
Output is correct |
2 |
Correct |
92 ms |
6424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
8776 KB |
Output is correct |
2 |
Correct |
136 ms |
7776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
7048 KB |
Output is correct |
2 |
Correct |
98 ms |
7112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
7044 KB |
Output is correct |
2 |
Correct |
121 ms |
7204 KB |
Output is correct |