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 <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][0] = 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;
}
}
//db(i, dp[i][0], dp[i][1]);
}
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();
}
# | 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... |