답안 #637118

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
637118 2022-08-31T15:23:53 Z Dec0Dedd DEL13 (info1cup18_del13) C++14
100 / 100
23 ms 6312 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5+100;
int d[N], par[N][3], cnt[N], n, q;
bool dp[N][3], vis[N][3];

bool gen(int i, int l) {
   if (i == q) {
      if (l == 0) return true;
      return false;
   }

   int x=d[i]-l;
   if (x < 0) return false;

   if (vis[i][l]) return dp[i][l];
   vis[i][l]=true;

   if (x%2 == 0) {
      if ((x == 0 || l > 0) && gen(i+1, 0)) par[i][l]=0, dp[i][l]=true;
      if (x >= 2 && gen(i+1, 2)) par[i][l]=2, dp[i][l]=true;
   } else {
      if (gen(i+1, 1)) par[i][l]=1, dp[i][l]=true;
   }

   return dp[i][l];
}

void solve(int c) {
   cin>>n>>q;

   if (q == 0) {
      cout<<-1<<"\n";
      return;
   }

   for (int i=0; i<=n+10; ++i) d[i]=0, cnt[i]=0;
   for (int i=0; i<=n+10; ++i) {
      for (int j=0; j<3; ++j) dp[i][j]=vis[i][j]=false, par[i][j]=-1;
   }

   vector<int> v;
   v.push_back(0);
   for (int i=0; i<q; ++i) {
      int p; cin>>p;
      v.push_back(p);
   }
   q+=2, v.push_back(n+1);
   for (int i=1; i<q; ++i) d[i]=cnt[i]=v[i]-v[i-1]-1;
   bool ok=gen(1, 0);

   if (!ok) {
      cout<<-1<<"\n";
   } else {

      int l=0;
      for (int i=1; i<q; ++i) {
         int x=par[i][l];
         cnt[i]-=x, cnt[i+1]-=x;
         l=par[i][l];
      }

      vector<int> mv;
      for (int i=1; i<q; ++i) {
         int l=v[i-1], k=cnt[i]/2;
         assert(cnt[i]%2 == 0);
         int x=l+2;
         while (k--) mv.push_back(x), x+=2; 
      }

      l=0;
      for (int i=1; i<q; ++i) {
         int x=par[i][l];
         while (x--) mv.push_back(v[i]);
         l=par[i][l];
      }

      cout<<mv.size()<<"\n";
      for (auto u : mv) cout<<u<<" ";
      cout<<"\n";
   }
}

int main() {
   int t, c=1; cin>>t;
   while (t--) solve(c++);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 10 ms 344 KB Output is correct
4 Correct 10 ms 368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 596 KB Output is correct
2 Correct 5 ms 2516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 10 ms 344 KB Output is correct
4 Correct 10 ms 368 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 10 ms 344 KB Output is correct
4 Correct 10 ms 368 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 17 ms 3156 KB Output is correct
9 Correct 18 ms 4216 KB Output is correct
10 Correct 16 ms 4284 KB Output is correct
11 Correct 23 ms 6312 KB Output is correct