#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;
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;
if(d[i-1]==0){
solve(l,i-2);
solve(i+1,r);
}
else{
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;
if(d[i+1]==0){
solve(l,i-1);
solve(i+2,r);
}
else{
solve(l,i-1);
solve(i+1,r);
}
}
else{
// cout<<"!!!\n";
ok=0;
}
}
}
if(!fin)ok=0;
return;
}
// cout<<l<<" "<<r<<"\n";
for(int i=l;i<=r;i+=2){
if(!mark[i]||!mark[i+1]){
if(!d[i]&&!d[i+1])continue;
if((d[i]<2||d[i+1]<2)){
// cout<<d[i]<<" "<<d[i+1]<<"\n";
// cout<<"!!\n";
ok=0;
}
d[i]-=2;
d[i+1]-=2;
ans.pb(a[i]);
ans.pb(a[i]);
mark[i]=mark[i+1]=1;
}
}
}
//0 0 2 4 2
//6
//15 15 10 10 4 4
//1 2 3 4 5 6 7 10 15
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;
}
}
// cout<<"!!\n";
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<<" "<<(n-q+1)/2<<"\n";
// for(int i=1;i<q;i++)cout<<a[i]<<" ";
// cout<<"\n";
cout<<(n-q+1)/2<<"\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:182:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
182 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
3 ms |
2396 KB |
Output is correct |
4 |
Correct |
3 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
3 ms |
2396 KB |
Output is correct |
4 |
Correct |
3 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
3 ms |
2396 KB |
Output is correct |
4 |
Correct |
3 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
5 ms |
2652 KB |
Output is correct |
9 |
Correct |
5 ms |
2652 KB |
Output is correct |
10 |
Correct |
4 ms |
2908 KB |
Output is correct |
11 |
Correct |
8 ms |
3548 KB |
Output is correct |