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 "/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 - flag) % 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);
}
}
}
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 |
---|
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... |