結果

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

ソースコード

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 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 ((a[i] < a[i + 1] && a[i + 1] < a[i + 2]) || (a[i] > a[i + 1] && 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() > 6) {
        cout << "No" << endl;
        return;
    }
    rep(i, x.size()) {
        rep(j, n) {
            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;
}

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