#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, inf = 1e9 + 1;
void solve(){
int n, q;
cin >> n >> q;
bool stay[n + 1]{};
for (int p, i = 0; i < q; i++) cin >> p, stay[p] = 1;
bool dp[n + 1][2]{};
int par[n + 1][2]{}, last[n + 1]{};
dp[0][0] = 1;
for (int i = 1; i <= n; i++){
if (stay[i]){
dp[i][0] |= dp[i - 1][0];
par[i][0] = i - 1;
last[i] = i;
continue;
}
last[i] = last[i - 1];
if (i >= 3 and !stay[i - 1] and !stay[i - 2] and dp[i - 2][0]){
par[i][0] = i - 2;
dp[i][0] = 1;
}
if (i >= 2 and !stay[i - 1] and (dp[i - 2][1] or dp[i - 2][0])){
par[i][1] = i - 2;
dp[i][1] = 1;
}
if (last[i] and i - last[i] <= last[i] - last[last[i] - 1] - 1){
int p = i - (i - last[i]) * 2 - 1;
assert(p >= 0);
if (dp[p][0] or dp[p][1]) {
dp[i][0] = 1;
par[i][0] = p;
}
}
}
if (!dp[n][0]) return void(cout << -1 << '\n');
deque<int> ans;
for (int i = n; i; ){
if (stay[i]) i = par[i][0];
else if (dp[i][0]){
if (par[i][0] == i - 2){
ans.push_front(i - 1);
}
else{
int mid = (i + par[i][0] + 1) / 2;
for (int j = 0; j < i - mid; j++) ans.push_back(mid);
}
i = par[i][0];
}
else{
ans.push_front(i);
i = par[i][1];
}
}
cout << ans.size() << '\n';
for (int i : ans) cout << i << ' ';
cout << '\n';
}
signed main(){
file;
int t;
cin >> t;
while (t--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
4 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
460 KB |
Output is correct |
2 |
Correct |
4 ms |
1484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
4 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
4 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
8 ms |
1740 KB |
Output is correct |
9 |
Correct |
9 ms |
1744 KB |
Output is correct |
10 |
Correct |
9 ms |
1740 KB |
Output is correct |
11 |
Correct |
13 ms |
2340 KB |
Output is correct |