#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define X first
#define Y second
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
for (; l != r; ++l)
cout << *l << " \n"[l + 1 == r];
}
#ifdef Doludu
#define test(x...) abc("[" + string(#x) + "]", x);
#else
#define test(x...) void(0);
#endif
const int N = 1001;
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector <int> a(m + 1, 0);
vector <int> rem;
for (int i = 1; i <= m; ++i) {
cin >> a[i];
rem.pb(a[i] - a[i - 1] - 1);
}
rem.pb(n - a[m]);
vector <vector <int>> dp(m + 1, vector <int>(3, false));
vector <vector <int>> rt(m + 1, vector <int>(3, -1));
dp[0][0] = true;
for (int i = 0; i < m; ++i) for (int f = 0; f <= 2; ++f) if (dp[i][f]) {
int left = rem[i] - f;
assert(left >= 0);
if (left == 0) {
dp[i + 1][0] = true;
rt[i + 1][0] = f;
continue;
}
if (left & 1) {
// optimal 1
if (rem[i + 1] >= 1)
dp[i + 1][1] = true, rt[i + 1][1] = f;
} else {
if (f) {
dp[i + 1][0] = true, rt[i + 1][0] = f;
}
if (rem[i + 1] >= 2)
dp[i + 1][2] = true, rt[i + 1][2] = f;
}
}
int now = -1;
for (int i = 1; i <= 2; ++i) if (dp[m][i] && rem[m] >= i && (rem[m] - i) % 2 == 0)
now = i;
if (dp[m][0] && rem[m] == 0)
now = 0;
if (now != -1) {
vector <int> move, fr;
for (int i = m; ~i; --i) {
for (int j = 0; j < now; ++j)
move.pb(a[i]), rem[i - 1]--, rem[i]--;
now = rt[i][now];
}
for (int i = 0; i <= m; ++i) {
assert(rem[i] % 2 == 0);
int mid;
if (i < m)
mid = a[i] + a[i + 1] >> 1;
else mid = a[i] + n + 1 >> 1;
for (int j = 0; j < rem[i] / 2; ++j) {
fr.pb(mid);
}
}
cout << fr.size() + move.size() << endl;
for (int i : fr)
cout << i << ' ';
for (int i : move)
cout << i << ' ';
cout << endl;
} else {
cout << -1 << endl;
}
}
}
Compilation message
del13.cpp: In function 'int main()':
del13.cpp:81:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | mid = a[i] + a[i + 1] >> 1;
del13.cpp:82:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
82 | else mid = a[i] + n + 1 >> 1;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
11 ms |
332 KB |
Output is correct |
4 |
Correct |
13 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
11 ms |
332 KB |
Output is correct |
4 |
Correct |
13 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
11 ms |
332 KB |
Output is correct |
4 |
Correct |
13 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
12 ms |
1080 KB |
Output is correct |
9 |
Correct |
13 ms |
2668 KB |
Output is correct |
10 |
Correct |
13 ms |
3008 KB |
Output is correct |
11 |
Correct |
19 ms |
5936 KB |
Output is correct |