//#include "/Users/stdc++.h"
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pii pair < int, int >
#define fs first
#define sc second
#define getfiles ifstream cin("input.txt"); ofstream cout("output.txt");
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
typedef long long ll;
typedef long double ld;
const int INF = 1e8;
const ll BIG_INF = 1e18;
const ll MOD = 1e9 + 7;
const int maxn = 2e5 + 5;
int a[maxn], dp[maxn][3], from[maxn][3], cnt[maxn];
vector < int > ans;
void upd(int i, int from_flag, int to_flag, int len) {
int cnt = len - from_flag - to_flag;
int new_val = dp[i][from_flag] + ((cnt % 2) ? INF : (cnt / 2 + to_flag));
if (new_val < dp[i + 1][to_flag]) {
dp[i + 1][to_flag] = new_val;
from[i + 1][to_flag] = from_flag;
}
}
void upd_ans(int l, int r, int cnt) {
int len = r - l + 1;
if (l > r || len < 3) return;
int i = l + 1;
cnt = (len - cnt) / 2;
while (cnt-- && i < r) {
ans.pb(i);
i += 2;
}
}
int main() {
fast_io
int t;
cin >> t;
while (t--) {
int n, q;
cin >> n >> q;
a[q + 1] = n + 1;
for (int i = 1; i <= q; i++)
cin >> a[i];
for (int i = 0; i <= q + 1; i++) {
for (int flag = 0; flag < 3; flag++) {
dp[i][flag] = INF;
from[i][flag] = -1;
}
}
dp[0][0] = 0;
for (int i = 0; i <= q; i++) {
int len = a[i + 1] - (a[i] + 1);
for (int flag = 0; flag < 3; flag++) {
if (flag >= len) {
if (flag == len) upd(i, flag, 0, len);
continue;
}
if (len % 2)
upd(i, flag, 1, len);
else {
upd(i, flag, 2, len);
if (flag) upd(i, flag, 0, len);
}
}
}
if (dp[q + 1][0] == INF) {
cout << "-1\n";
continue;
}
cnt[q + 1] = 0;
int ind = from[q + 1][0];
for (int i = q; i >= 0; i--) {
cnt[i] = ind;
ind = from[i][ind];
}
for (int i = 0; i <= q; i++)
upd_ans(a[i] + 1, a[i + 1] - 1, cnt[i] + cnt[i + 1]);
for (int i = 1; i <= q; i++)
for (int j = 0; j < cnt[i]; j++)
ans.pb(a[i]);
cout << sz(ans) << "\n";
for (int i = 0; i < sz(ans); i++)
cout << ans[i] << " ";
cout << "\n";
ans.clear();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
380 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
380 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
8 ms |
376 KB |
Output isn't correct |
4 |
Incorrect |
8 ms |
376 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
504 KB |
Output isn't correct |
2 |
Incorrect |
6 ms |
376 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
380 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
8 ms |
376 KB |
Output isn't correct |
4 |
Incorrect |
8 ms |
376 KB |
Output isn't correct |
5 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
6 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
7 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
380 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
8 ms |
376 KB |
Output isn't correct |
4 |
Incorrect |
8 ms |
376 KB |
Output isn't correct |
5 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
6 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
7 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
8 |
Incorrect |
10 ms |
504 KB |
Output isn't correct |
9 |
Incorrect |
11 ms |
1016 KB |
Output isn't correct |
10 |
Incorrect |
11 ms |
888 KB |
Output isn't correct |
11 |
Incorrect |
11 ms |
1564 KB |
Output isn't correct |