結果

問題 No.1369 交換門松列・竹
ユーザー simansiman
提出日時 2022-11-16 07:31:37
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 2,007 bytes
コンパイル時間 4,829 ms
コンパイル使用メモリ 132,348 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 07:02:01
合計ジャッジ時間 5,025 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 11 ms
4,348 KB
testcase_14 AC 15 ms
4,348 KB
testcase_15 AC 16 ms
4,348 KB
testcase_16 AC 17 ms
4,348 KB
testcase_17 AC 18 ms
4,348 KB
testcase_18 AC 16 ms
4,348 KB
testcase_19 AC 15 ms
4,348 KB
testcase_20 AC 19 ms
4,348 KB
testcase_21 AC 15 ms
4,348 KB
testcase_22 AC 19 ms
4,348 KB
testcase_23 AC 16 ms
4,348 KB
testcase_24 AC 19 ms
4,348 KB
testcase_25 AC 14 ms
4,348 KB
testcase_26 AC 13 ms
4,348 KB
testcase_27 AC 14 ms
4,348 KB
testcase_28 AC 13 ms
4,348 KB
testcase_29 AC 13 ms
4,348 KB
testcase_30 AC 17 ms
4,348 KB
testcase_31 AC 17 ms
4,348 KB
testcase_32 AC 4 ms
4,348 KB
testcase_33 AC 9 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

bool is_kadomatsu(int a, int b, int c) {
  if (a == b) return false;
  if (a == c) return false;
  if (b == c) return false;
  if (a < b && b < c) return false;
  if (a > b && b > c) return false;

  return true;
}

void solver(int N, vector<int> A) {
  int idx = -1;
  vector<int> check_list;

  for (int i = 0; i < N - 2; ++i) {
    int a = A[i];
    int b = A[i + 1];
    int c = A[i + 2];

    if (is_kadomatsu(a, b, c)) continue;
    check_list.push_back(i);
    idx = i;
  }

  assert(idx != -1);
  for (int i = idx; i < min(idx + 3, N); ++i) {
    for (int j = 0; j < N; ++j) {
      swap(A[i], A[j]);

      bool ok = true;
      for (int cid : check_list) {
        if (cid <= i && i <= cid + 2) {
          ok &= is_kadomatsu(A[cid], A[cid + 1], A[cid + 2]);
        } else if (cid <= j && j <= cid + 2) {
          ok &= is_kadomatsu(A[cid], A[cid + 1], A[cid + 2]);
        } else {
          ok = false;
        }
        if (not ok) break;
      }
      if (i - 2 >= 0) ok &= is_kadomatsu(A[i - 2], A[i - 1], A[i]);
      if (i - 1 >= 0 && i + 1 < N) ok &= is_kadomatsu(A[i - 1], A[i], A[i + 1]);
      if (i + 2 < N) ok &= is_kadomatsu(A[i], A[i + 1], A[i + 2]);
      if (j - 2 >= 0) ok &= is_kadomatsu(A[j - 2], A[j - 1], A[j]);
      if (j - 1 >= 0 && j + 1 < N) ok &= is_kadomatsu(A[j - 1], A[j], A[j + 1]);
      if (j + 2 < N) ok &= is_kadomatsu(A[j], A[j + 1], A[j + 2]);

      if (ok) {
        /*
        */
        cout << "Yes" << endl;
        return;
      }

      swap(A[i], A[j]);
    }
  }

  cout << "No" << endl;
}

int main() {
  int T;
  cin >> T;

  for (int t = 0; t < T; ++t) {
    int N;
    cin >> N;
    vector<int> A(N);

    for (int i = 0; i < N; ++i) {
      cin >> A[i];
    }

    solver(N, A);
  }

  return 0;
}
0