結果

問題 No.3726 Flawless Flow
コンテスト
ユーザー tnakao0123
提出日時 2026-09-21 13:07:14
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,581 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 375 ms
コンパイル使用メモリ 83,708 KB
実行使用メモリ 9,940 KB
最終ジャッジ日時 2026-09-21 13:07:24
合計ジャッジ時間 3,325 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28 WA * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 3726.cc:  No.3726 Flawless Flow - yukicoder
 */

#include<cstdio>
#include<algorithm>
#include<utility>
#include<functional>

using namespace std;

/* constant */

const int MAX_N = 500;

/* typedef */

using pii = pair<int,int>;

/* global variables */

int as[MAX_N], bs[MAX_N];
int bps[MAX_N], cps[MAX_N + 1][MAX_N];
pii ps[MAX_N];
bool fs[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", as + i), as[i]--;
  for (int i = 0; i < n; i++) scanf("%d", bs + i), bs[i]--;

  for (int i = 0; i < n; i++) bps[bs[i]] = i;
  for (int i = 0; i < n; i++) cps[0][i] = bps[as[i]];
  //for (int i = 0; i < n; i++) printf(" %d", cps[0][i]); putchar('\n');
  
  for (int i = 1; i <= n; i++) {
    copy(cps[i - 1], cps[i - 1] + n, cps[i]);
    
    for (int j = 0; j < n; j++) ps[j] = {abs(j - cps[i - 1][j]), j};
    sort(ps, ps + n, greater<pii>());
    fill(fs, fs + n, false);

    for (int k = 0; k < n && ps[k].first > 0; k++) {
      int j = ps[k].second;
      if (cps[i - 1][j] > j) {
	if (! fs[j] && ! fs[j + 1]) {
	  swap(cps[i][j], cps[i][j + 1]);
	  fs[j] = fs[j + 1] = true;
	}
      }
      else {
	if (! fs[j] && ! fs[j - 1]) {
	  swap(cps[i][j], cps[i][j - 1]);
	  fs[j] = fs[j - 1] = true;
	}
      }
    }
  }

  for (int j = 0; j + 1 < n; j++)
    if (cps[n][j] > cps[n][j + 1]) {
      puts("-1");
      return 0;
    }

  for (int i = 0; i <= n; i++)
    for (int j = 0; j < n; j++)
      printf("%d%c", bs[cps[i][j]] + 1, (j + 1 < n) ? ' ' : '\n');
  
  return 0;
}

0