#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
using namespace std;
const int maxN = 18;
int T, N, M, l2[1 << maxN], fromst[1 << maxN], del[1 << maxN];
bool pos[1 << maxN];
void solve(){
cin >> M;
vector<int> has, wok;
has.resize(M);
fill(pos, pos + (1 << N), false);
for(int i = 0; i < M; i++){
cin >> has[i];
has[i]--;
}
queue<int> que;
que.push((1 << N) - 1);
pos[(1 << N) - 1] = true;
int curst = 0;
for(int x : has) curst ^= (1 << x);
bool f = false;
while(que.size()){
if(f) break;
int i = que.front();
que.pop();
vector<int>().swap(wok);
int x = i;
while(x){
wok.push_back(l2[(x & -x)]);
x -= (x & -x);
}
//for(int x : wok) cout << x << " ";
//cout << endl;
if(wok.size() >= 3){
for(int j = 1; j < wok.size() - 1; j++){
int n = i ^ (1 << wok[j - 1]) ^ (1 << wok[j + 1]);
if(pos[n] || ((n & curst) != curst)) continue;
pos[n] = true;
que.push(n);
fromst[n] = i;
del[n] = wok[j];
if(n == curst){
f = true;
break;
}
}
}
}
//cout << "curst = " << curst << endl;
if(!pos[curst]){
cout << -1 << endl;
return;
}
vector<int> ans;
vector<int>().swap(ans);
while(curst != ((1 << N) - 1)){
ans.push_back(del[curst]);
curst = fromst[curst];
}
reverse(ans.begin(), ans.end());
cout << ans.size() << endl;
for(int x : ans) cout << x + 1 << " ";
cout << endl;
}
void solve2(){
cin >> M;
bool has[100000];
vector<int> ans;
vector<int>().swap(ans);
int x, cnt = 0;
for(int i = 0; i < M; i++){
cin >> x;
x--;
has[x] = true;
cnt++;
}
int k = -1;
for(int i = 1; i < N - 1; i++){
if(has[i] && !has[i - 1] && !has[i + 1]){
k = i;
}
}
if(k == -1 && cnt != N){
cout << -1 << endl;
return;
} else if(k == -1){
cout << 0 << endl;
return;
}
//cout << "k = " << k << endl;
deque<int> l, r;
for(int i = k - 1; i >= 0 && !has[i]; i--){
//cout << "i = " << i << endl;
l.push_front(i);
}
for(int i = k + 1; i < N && !has[i]; i++){
//cout << "i = " << i << endl;
r.push_back(i);
}
while(l.size() > 2){
l.pop_front();
int b = l.front();
l.pop_front();
l.pop_front();
ans.push_back(b);
l.push_front(b);
}
while(r.size() > 2){
r.pop_front();
int b = r.front();
r.pop_front();
r.pop_front();
ans.push_back(b);
r.push_front(b);
}
if(l.size() != r.size()){
cout << -1 << endl;
}
while(l.size()){
l.pop_back();
r.pop_back();
ans.push_back(k);
}
cout << ans.size() << endl;
for(int x : ans) cout << x + 1 << " ";
cout << endl;
}
int main(){
for(int i = 0; i < maxN; i++) l2[1 << i] = i;
cin >> T;
while(T--){
cin >> N;
if(N <= 18)
solve();
else solve2();
}
}
Compilation message
del13.cpp: In function 'void solve()':
del13.cpp:40:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int j = 1; j < wok.size() - 1; j++){
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
245 ms |
1152 KB |
Output is correct |
4 |
Correct |
416 ms |
1812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
1952 KB |
Output isn't correct |
2 |
Incorrect |
14 ms |
1532 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
245 ms |
1152 KB |
Output is correct |
4 |
Correct |
416 ms |
1812 KB |
Output is correct |
5 |
Incorrect |
2 ms |
512 KB |
Output isn't correct |
6 |
Incorrect |
2 ms |
896 KB |
Output isn't correct |
7 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
245 ms |
1152 KB |
Output is correct |
4 |
Correct |
416 ms |
1812 KB |
Output is correct |
5 |
Incorrect |
2 ms |
512 KB |
Output isn't correct |
6 |
Incorrect |
2 ms |
896 KB |
Output isn't correct |
7 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
8 |
Incorrect |
29 ms |
1276 KB |
Output isn't correct |
9 |
Incorrect |
31 ms |
384 KB |
Output isn't correct |
10 |
Incorrect |
31 ms |
1024 KB |
More than N moves |
11 |
Incorrect |
26 ms |
512 KB |
Output isn't correct |