#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define int ll
#define y1 yy
#define maksim gay
#define ppb pop_back
#define gcd(a,b) __gcd(a,b)
#define in insert
const int dx[4]={-1,0,1,0};
const int dy[4]={0,-1,0,1};
const int inf=1e16;
const int N=5e4+100;
const int MAX=1e5+10;
const int mod=1e9+7;
int n,q;
int a[MAX],d[MAX];
int mark[MAX];
vector<int> ans;
bool ok;
//1 2 3 4 5 6 7 8 9 10 11 12
void solve(int l,int r){
if(l>r)return;
// cout<<l<<" "<<r<<"\n";
if((r-l+1)%2==1){
bool fin=0;
for(int i=l;i<=r;i+=2){
if(d[i]>=2){
if(mark[i]){
fin=1;
solve(l,i-1);
solve(i+1,r);
}
else if(d[i-1]>=2){
fin=1;
ans.pb(a[i-1]);
ans.pb(a[i-1]);
d[i]-=2;
d[i-1]-=2;
mark[i]=mark[i-1]=1;
solve(l,i-1);
solve(i+1,r);
}
else if(d[i+1]>=2){
fin=1;
d[i]-=2;
d[i+1]-=2;
ans.pb(a[i]);
ans.pb(a[i]);
mark[i]=mark[i+1]=1;
solve(l,i-1);
solve(i+1,r);
}
else{
// cout<<"!!!\n";
ok=0;
}
}
}
if(!fin)ok=0;
return;
}
for(int i=l;i<=r;i+=2){
if(!mark[i]||!mark[i+1]){
if(d[i]<2||d[i+1]<2){
ok=0;
}
d[i]-=2;
d[i+1]-=2;
ans.pb(a[i]);
ans.pb(a[i]);
mark[i]=mark[i+1]=1;
}
}
}
void solve(){
cin>>n>>q;
for(int i=0;i<=q+10;i++){
mark[i]=0;
d[i]=0;
}
ok=1;
a[0]=0;
a[q+1]=n+1;
ans.clear();
for(int i=1;i<=q;i++){
cin>>a[i];
}
for(int i=1;i<=q;i++){
d[i]=a[i]-a[i-1]-1;
}
d[q+1]=a[q+1]-a[q]-1;
q++;
for(int i=1;i<q;i++){
if(d[i]%2){
if(d[i+1]==0){
cout<<-1<<"\n";
return;
}
ans.pb(a[i]);
d[i]--;
d[i+1]--;
mark[i]=mark[i+1]=1;
}
}
for(int i=1;i<=q;i++){
if(d[i]%2==1){
cout<<-1<<"\n";
return;
}
}
int l=0;
for(int i=1;i<=q;i++){
if(!d[i]){
solve(l+1,i-1);
l=i;
}
}
solve(l+1,q);
if(!ok){
cout<<-1<<"\n";
return;
}
for(int i=0;i<q;i++){
int l=a[i]+1;
int r=l+d[i+1];
int m=(l+r)/2;
while(d[i+1]){
d[i+1]-=2;
ans.pb(m);
}
}
reverse(all(ans));
// cout<<1<<"\n";
cout<<(n-q+1)/2<<"\n";
// cout<<sz(ans)<<"\n";
for(int x:ans)cout<<x<<" ";
cout<<"\n";
}
main(){
// freopen("prizes.in", "r", stdin);
// freopen("prizes.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--){
solve();
}
}
Compilation message
del13.cpp:162:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
162 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Incorrect |
4 ms |
2396 KB |
Output isn't correct |
4 |
Incorrect |
3 ms |
2396 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Incorrect |
4 ms |
2396 KB |
Output isn't correct |
4 |
Incorrect |
3 ms |
2396 KB |
Output isn't correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Incorrect |
4 ms |
2396 KB |
Output isn't correct |
4 |
Incorrect |
3 ms |
2396 KB |
Output isn't correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Incorrect |
6 ms |
2652 KB |
Output isn't correct |
9 |
Incorrect |
5 ms |
2820 KB |
Output isn't correct |
10 |
Incorrect |
4 ms |
2904 KB |
Output isn't correct |
11 |
Incorrect |
7 ms |
3292 KB |
Output isn't correct |