結果

問題 No.1369 交換門松列・竹
ユーザー boutarouboutarou
提出日時 2021-01-29 22:47:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 2,481 bytes
コンパイル時間 2,630 ms
コンパイル使用メモリ 205,024 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-09 16:25:48
合計ジャッジ時間 4,296 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 14 ms
4,376 KB
testcase_14 AC 19 ms
4,376 KB
testcase_15 AC 22 ms
4,380 KB
testcase_16 AC 33 ms
4,376 KB
testcase_17 AC 34 ms
4,380 KB
testcase_18 AC 31 ms
4,376 KB
testcase_19 AC 22 ms
4,380 KB
testcase_20 AC 152 ms
4,376 KB
testcase_21 AC 22 ms
4,376 KB
testcase_22 AC 187 ms
4,376 KB
testcase_23 AC 32 ms
4,380 KB
testcase_24 AC 14 ms
4,380 KB
testcase_25 AC 96 ms
4,376 KB
testcase_26 AC 87 ms
4,376 KB
testcase_27 AC 95 ms
4,380 KB
testcase_28 AC 8 ms
4,380 KB
testcase_29 AC 9 ms
4,376 KB
testcase_30 AC 16 ms
4,376 KB
testcase_31 AC 16 ms
4,380 KB
testcase_32 AC 7 ms
4,380 KB
testcase_33 AC 35 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < int(n); i++)
using ll = long long;
using P = pair<int, int>;

bool kadomatsu(ll a, ll b, ll c) {
    if (a != b && b != c && c != a && (max({a, b, c}) == b || min({a, b, c}) == b)) return true;
    return false;
}

bool kadomatsux(vector<int> &a) {
    rep(i, (int)a.size() - 2) {
        if (!kadomatsu(a[i], a[i + 1], a[i + 2])) {
            return false;
        }
    }
    return true;
}

bool kadomatsu(vector<int> &a, int x) {
    for (int i = x - 1; i <= x + 1; i++) {
        if (1 <= i && i <= (int)a.size() - 2) {
            if (!kadomatsu(a[i - 1], a[i], a[i + 1])) return false;
        }
    }
    return true;
}

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    vector<int> x;
    rep(i, n - 2) {
        if (!kadomatsu(a[i], a[i + 1], a[i + 2])) {
            x.push_back(i);
            x.push_back(i + 1);
            x.push_back(i + 2);
        }
    }
    x.erase(unique(x.begin(), x.end()), x.end());
    if ((int)x.size() > 20) {
        cout << "No" << endl;
        return;
    }
    rep(i, x.size()) {
        rep(j, n) {
            if(x[i] == j) continue;
            swap(a[x[i]], a[j]);
            bool ok = true;
            rep(k, x.size()) {
                if (!kadomatsu(a, x[k])) ok = false;
            }
            if (!kadomatsu(a, j)) ok = false;
            if (ok) {
                cout << "Yes" << endl;
                return;
            }
            swap(a[x[i]], a[j]);
        }
    }
    cout << "No" << endl;
}

void solve2() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) {
        cin >> a[i];
    }
    rep(i, n) {
        rep(j, n) {
            if (i == j) continue;
            swap(a[i], a[j]);
            // if (i == 1 && j == 2) {
            //     cout << "#" << endl;
            //     kadomatsu(a);
            //     rep(k, n - 2) {
            //         cout << kadomatsu(a[k], a[k + 1], a[k + 2]) << " ";
            //     }
            //     cout << kadomatsu(a) << endl;
            //     cout << endl;
            //     return;
            // }
            if (kadomatsux(a)) {
                cout << "Yes" << endl;
                return;
            }
            swap(a[i], a[j]);
        }
    }
    cout << "No" << endl;
    return;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
        // solve2();
    }
    return 0;
}
0