//#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 &val_to_upd, int &from, int i, int flag, int val) {
int new_val = dp[i][flag] + val;
if (new_val < val_to_upd) {
val_to_upd = new_val;
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(dp[i + 1][0], from[i + 1][0], i, flag, 0);
continue;
}
int cnt_oper = (len - flag) / 2;
if (len % 2)
upd(dp[i + 1][1], from[i + 1][1], i, flag, cnt_oper + 1);
else {
upd(dp[i + 1][2], from[i + 1][2], i, flag, cnt_oper + 1);
if (flag) upd(dp[i + 1][0], from[i + 1][0], i, flag, cnt_oper);
}
}
}
int ind = from[q + 1][0];
if (dp[q][ind] == INF) {
cout << "-1\n";
continue;
}
cnt[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 |
376 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 |
376 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
8 ms |
504 KB |
Output isn't correct |
4 |
Incorrect |
9 ms |
536 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
632 KB |
Output isn't correct |
2 |
Incorrect |
10 ms |
1016 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
8 ms |
504 KB |
Output isn't correct |
4 |
Incorrect |
9 ms |
536 KB |
Output isn't correct |
5 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
6 |
Incorrect |
6 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 |
376 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
8 ms |
504 KB |
Output isn't correct |
4 |
Incorrect |
9 ms |
536 KB |
Output isn't correct |
5 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
6 |
Incorrect |
6 ms |
376 KB |
Output isn't correct |
7 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
8 |
Incorrect |
14 ms |
1400 KB |
Output isn't correct |
9 |
Incorrect |
12 ms |
1276 KB |
Output isn't correct |
10 |
Incorrect |
16 ms |
1784 KB |
Output isn't correct |
11 |
Incorrect |
13 ms |
1912 KB |
Output isn't correct |