QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#882900#9641. Two permutationshos_lyric#0 549ms18164kbC++143.7kb2025-02-05 13:03:282025-02-05 13:03:38

Judging History

This is the latest submission verdict.

  • [2025-02-05 13:03:38]
  • Judged
  • Verdict: 0
  • Time: 549ms
  • Memory: 18164kb
  • [2025-02-05 13:03:28]
  • Submitted

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


vector<int> calc(const vector<int> &as) {
  const int N = (int)as.size() / 2;
  vector<int> ls(N, -1), rs(N, -1);
  for (int i = 0; i <   N; ++i) ls[as[i]] = i;
  for (int i = N; i < 2*N; ++i) rs[as[i]] = i;
  vector<int> bs(N, 0);
  for (int a = 0; a < N; ++a) {
    int f = 0;
    for (int i = ls[a]; i <= rs[a]; ++i) f ^= 1 << as[i];
    bs[a] = __builtin_ctz(~f);
  }
  return bs;
}

void exper() {
  for (int N = 1; N <= 6; ++N) {
    map<vector<int>, vector<vector<int>>> ma;
    vector<int> as(2*N);
    for (int i = 0; i < 2*N; ++i) as[i] = i % N;
    do {
      do {
        const auto bs = calc(as);
        // cout << as << ": " << bs << '\n';
        ma[bs].push_back(as);
      } while (next_permutation(as.begin() + N, as.end()));
    } while (next_permutation(as.begin(), as.begin() + N));
    cerr << "N = " << N << ": |ma| = " << ma.size() << endl;
    if (N <= 5) {
      for (const auto &kv : ma) {
        cout << kv.first << " " << kv.second.size() << " " << kv.second << '\n';
      }
    }
  }
}

vector<int> solve(const vector<int> &P) {
  const int N = P.size();
  vector<vector<int>> graph(N);
  for (int u = 0; u < N; ++u) {
    if (0 <= P[u] && P[u] <= u) {
      if (P[u] < u) graph[P[u]].push_back(u);
    } else {
      return {};
    }
  }
  vector<int> que;
  vector<int> dep(N, 0);
  for (int u = 0; u < N; ++u) if (P[u] == u) {
    que.push_back(u);
  }
  for (int j = 0; j < N; ++j) {
    const int u = que[j];
    for (const int v : graph[u]) {
      dep[v] = dep[u] + 1;
      que.push_back(v);
    }
  }
  vector<int> ans;
  for (int s = 0; s < 2; ++s) {
    if (s) {
      for (int u = 0; u < N; ++u) if (P[u] == u) ans.push_back(u);
    }
    for (const int u : que) if ((dep[u] & 1) == s) {
      for (const int v : graph[u]) ans.push_back(v);
      ans.push_back(u);
    }
  }
  return ans;
}

int main() {
  // exper();
  
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    int N;
    scanf("%d", &N);
    vector<int> P(N);
    for (int u = 0; u < N; ++u) {
      scanf("%d", &P[u]);
      --P[u];
    }
    
    const auto ans = solve(P);
    if (ans.size()) {
      puts("Yes");
      for (int i = 0; i < 2*N; ++i) {
        if (i) printf(" ");
        printf("%d", ans[i] + 1);
      }
      puts("");
    } else {
      puts("No");
    }
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3840kb

input:

10
1
1
2
1 1
3
1 2 2
4
1 1 3 2
5
1 2 1 2 1
2
1 1
1
1
3
1 2 2
1
1
5
1 1 1 3 2

output:

Yes
1 1
Yes
2 1 1 2
Yes
1 3 2 1 2 3
Yes
2 1 3 4 1 3 4 2
Yes
3 5 1 4 2 1 2 3 5 4
Yes
2 1 1 2
Yes
1 1
Yes
1 3 2 1 2 3
Yes
1 1
Yes
2 3 1 5 4 1 5 2 4 3

result:

wrong answer Wrong Answer![4]

Subtask #2:

score: 0
Wrong Answer

Test #4:

score: 0
Wrong Answer
time: 1ms
memory: 3968kb

input:

10
10
1 2 3 4 3 1 2 4 3 8
10
1 2 1 1 4 6 4 5 2 9
10
1 2 2 4 5 3 7 7 2 3
10
1 1 2 4 5 4 3 5 7 7
10
1 2 2 4 4 1 6 6 6 10
10
1 2 2 4 1 3 7 7 1 10
10
1 2 3 4 2 1 3 3 1 7
10
1 1 1 4 5 1 3 5 4 7
10
1 2 2 1 1 2 6 5 8 5
10
1 2 2 2 5 3 6 2 4 5

output:

Yes
6 1 7 2 5 9 3 8 4 10 1 2 3 4 6 7 5 9 10 8
Yes
3 4 1 9 2 6 8 5 7 10 1 2 6 3 5 7 4 10 9 8
Yes
1 3 9 2 4 5 8 7 6 10 1 2 4 5 7 6 10 3 9 8
Yes
2 1 6 4 8 5 7 3 9 10 1 4 5 3 2 6 8 9 10 7
Yes
6 1 3 2 5 4 10 7 8 9 1 2 4 10 7 8 9 6 3 5
Yes
5 9 1 3 2 4 8 7 10 6 1 2 4 7 10 5 9 6 3 8
Yes
6 9 1 5 2 7 8 3 4 10...

result:

wrong answer Wrong Answer![4]

Subtask #3:

score: 0
Wrong Answer

Test #7:

score: 15
Accepted
time: 137ms
memory: 13696kb

input:

10
177329
1 1 2 2 1 4 1 2 3 3 2 3 4 1 2 3 3 2 2 2 2 3 4 3 1 2 4 2 4 1 4 2 2 4 1 3 3 4 1 1 1 3 2 4 3 3 2 4 2 2 4 1 1 4 3 1 4 4 4 3 1 4 1 3 2 1 2 3 2 4 2 4 4 2 1 3 2 2 2 3 3 1 4 1 3 1 2 4 1 4 2 3 4 3 1 4 4 1 2 2 3 3 4 1 4 4 3 4 3 2 1 2 4 3 1 1 4 4 4 2 3 4 3 3 3 1 4 4 1 1 2 4 4 1 3 4 4 4 2 1 2 2 1 2 3 ...

output:

Yes
2 5 7 14 25 30 35 39 40 41 52 53 56 61 63 66 75 82 84 86 89 95 98 104 111 115 116 126 129 130 134 140 143 151 152 154 155 166 173 174 175 179 180 182 190 192 206 207 209 214 221 224 226 235 239 242 243 246 252 256 259 261 274 277 278 288 289 291 292 294 295 297 300 305 307 310 312 314 320 321 32...

result:

ok Correct!

Test #8:

score: 15
Accepted
time: 169ms
memory: 14672kb

input:

10
42861
1 2 1 2 3 1 2 2 1 1 3 4 3 1 2 3 2 2 3 2 3 4 2 2 2 4 2 3 1 2 1 4 4 2 4 3 1 1 4 3 1 1 4 1 1 4 3 3 1 3 3 3 2 4 4 1 3 2 1 1 1 1 1 1 4 3 1 1 2 4 1 1 2 4 2 3 4 4 4 2 4 3 3 3 2 1 3 4 4 3 1 3 4 4 4 1 4 4 1 3 2 4 4 3 3 4 3 3 4 3 1 4 2 1 2 3 2 4 3 4 1 2 3 4 2 4 1 3 4 1 1 1 4 3 4 2 1 4 3 1 3 3 2 1 1 4...

output:

Yes
3 6 9 10 14 29 31 37 38 41 42 44 45 49 56 59 60 61 62 63 64 67 68 71 72 86 91 96 99 111 114 121 127 130 131 132 137 140 144 145 152 158 160 174 178 184 186 189 192 196 199 212 228 230 233 235 243 247 248 249 250 255 260 270 271 274 282 287 289 296 298 306 309 311 313 315 317 318 319 323 326 338 ...

result:

ok Correct!

Test #9:

score: 0
Wrong Answer
time: 133ms
memory: 15440kb

input:

10
45364
1 1 1 1 1 1 1 2 2 2 2 1 2 4 3 3 1 4 1 3 2 2 3 1 2 2 3 2 3 3 3 2 3 3 2 3 2 1 1 3 4 2 2 4 4 3 1 2 4 1 3 4 4 1 3 4 1 4 3 2 1 2 3 3 4 4 1 3 3 3 2 3 4 4 2 1 2 1 2 1 1 2 2 2 3 1 3 3 1 4 1 3 1 2 3 4 2 2 3 3 1 3 3 4 4 4 4 1 1 4 4 1 4 2 2 1 1 4 3 3 4 3 4 4 4 4 4 1 4 4 1 4 3 1 3 1 3 3 4 1 1 2 1 2 1 3...

output:

Yes
2 3 4 5 6 7 12 17 19 24 38 39 47 50 54 57 61 67 76 78 80 81 86 89 91 93 101 108 109 112 116 117 128 131 134 136 140 141 143 145 154 155 156 159 163 177 178 180 183 198 205 208 210 213 218 220 224 225 226 235 242 245 261 267 274 276 277 278 279 284 285 286 289 303 307 313 317 322 327 331 335 338 ...

result:

wrong answer Wrong Answer![4]

Subtask #4:

score: 0
Wrong Answer

Test #13:

score: 15
Accepted
time: 363ms
memory: 14096kb

input:

10
199850
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:

Yes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...

result:

ok Correct!

Test #14:

score: 0
Wrong Answer
time: 386ms
memory: 15484kb

input:

10
199997
1 1 1 4 4 6 4 8 4 10 11 10 6 14 15 16 17 15 19 15 14 4 15 10 25 26 27 1 29 30 31 26 33 15 30 36 37 38 39 38 33 4 10 44 25 33 33 11 49 50 31 1 53 54 55 14 50 58 8 49 36 6 63 39 65 6 33 10 69 29 19 72 29 74 1 76 63 49 4 80 50 82 83 15 4 86 87 50 89 36 27 89 93 11 95 63 1 37 99 100 101 102 10...

output:

Yes
2 3 28 52 75 97 1928 5175 12332 20407 47532 1 5 7 9 22 42 79 85 287 4 13 62 66 376 1181 8445 10507 12078 6 59 1417 22906 54276 70223 8 12 24 43 68 161 164 7528 7785 19705 35930 118069 10 48 94 1246 1820 7584 9033 14596 177269 11 21 56 178 264 623 717 4906 38122 163038 172258 14 18 20 23 34 84 10...

result:

wrong answer Wrong Answer![4]

Subtask #5:

score: 0
Wrong Answer

Test #19:

score: 0
Wrong Answer
time: 549ms
memory: 18164kb

input:

10
199755
1 2 1 3 5 1 6 7 2 1 5 6 10 9 12 2 4 14 2 18 5 17 23 4 6 7 4 6 20 27 16 24 29 8 33 26 33 27 17 39 5 33 34 41 4 16 24 19 42 8 29 48 48 27 37 24 43 13 4 28 38 27 38 23 29 55 14 62 26 9 62 20 35 31 57 74 69 5 26 60 33 59 5 19 21 45 15 87 3 54 17 2 57 36 18 67 25 62 47 53 89 27 5 59 75 77 29 9 ...

output:

Yes
3 6 10 1026 1098 1218 2009 3029 4012 5130 5454 5960 9031 9337 24561 1 9 16 19 92 1056 1176 7270 10434 13675 33916 53307 2 11 21 41 78 83 103 733 831 2496 7826 61670 5 64 143 1347 2037 2188 5908 11111 88595 147680 23 311 390 1073 4419 61152 63244 78093 83783 285 926 2347 3059 3873 12492 16075 179...

result:

wrong answer Wrong Answer![4]