#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define mp make_pair
using namespace std;
void db() {cout << endl;}
template <typename T, typename ...U> void db(T a, U ...b) {
//return;
cout << a << ' ', db(b...);
}
#ifdef Cloud
#define file freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#else
#define file ios::sync_with_stdio(false); cin.tie(0)
#endif
const int N = 3001;
void solve(){
int n, q;
cin >> n >> q;
bool stay[n + 1]{}, last[n + 1]{};
for (int p, i = 0; i < q; i++) cin >> p, stay[p] = 1;
bool find = 0;
for (int i = n; i >= 0; i--){
if (stay[i]) continue;
if (!find) last[i] = 1, find = 1;
else last[i] |= last[i + 1];
}
set<pair<int, int>> s;
for (int i = 1; i <= n; i++) s.insert({i, stay[i]});
while (!s.empty() and (*(--s.end())).se) s.erase(--s.end());
deque<int> ans;
vector<int> v;
for (auto i : s){
if (last[i.fi]) v.push_back(i.fi);
else if (i.se == 0) {
auto it = s.upper_bound(i);
auto it2 = s.lower_bound(i);
if (it == s.end() or it2 == s.begin()) continue;
it2--;
auto u = *it2, v = *it;
if (u.se or v.se) continue;
ans.push_back(i.fi);
s.erase(u);
s.erase(v);
}
else for (int t = 0; t < 2; t++){
auto it = s.upper_bound(i);
auto it2 = s.lower_bound(i);
if (it == s.end() or it2 == s.begin()) continue;
it2--;
auto u = *it2, v = *it;
if (u.se or v.se) continue;
ans.push_back(i.fi);
s.erase(u);
s.erase(v);
}
}
for (auto i : s) if (i.se == 0 and !last[i.fi]) return void(cout << -1 << '\n');
if (!v.empty() and (v.size() % 2 or v[0] == 1)) return void(cout << -1 << '\n');
if (!v.empty() and stay[v[0] - 1]) return void(cout << -1 << '\n');
if (!v.empty()) for (int i = 0; i < v.size(); i += 2) ans.push_front(v[i]);
cout << ans.size() << '\n';
for (int i : ans) cout << i << ' ';
cout << '\n';
}
signed main(){
file;
int t;
cin >> t;
while (t--)
solve();
}
Compilation message
del13.cpp: In function 'void solve()':
del13.cpp:62:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if (!v.empty()) for (int i = 0; i < v.size(); i += 2) ans.push_front(v[i]);
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
8 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
8 ms |
364 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
688 KB |
Output is correct |
2 |
Correct |
41 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
8 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
8 ms |
364 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
6 |
Incorrect |
2 ms |
204 KB |
Output isn't correct |
7 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
8 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
8 ms |
364 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
6 |
Incorrect |
2 ms |
204 KB |
Output isn't correct |
7 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
8 |
Incorrect |
51 ms |
4980 KB |
Output isn't correct |
9 |
Incorrect |
54 ms |
5084 KB |
Output isn't correct |
10 |
Incorrect |
60 ms |
5000 KB |
Output isn't correct |
11 |
Incorrect |
65 ms |
5388 KB |
Output isn't correct |