結果

問題 No.1369 交換門松列・竹
ユーザー boutarou
提出日時 2021-01-29 22:47:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 2,481 bytes
コンパイル時間 2,106 ms
コンパイル使用メモリ 200,400 KB
最終ジャッジ日時 2025-01-18 09:38:53
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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