結果

問題 No.1369 交換門松列・竹
ユーザー ChanyuhChanyuh
提出日時 2021-01-31 17:58:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 2,990 bytes
コンパイル時間 1,561 ms
コンパイル使用メモリ 132,152 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-11 11:59:01
合計ジャッジ時間 3,315 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}

int n;
vector<int> a;

void solve(){
    cin >> n;a.resize(n);
    rep(i,n) cin >> a[i];
    vector<bool> f(n,false);
    vector<int> ng={};
    rep(i,n-2){
        int ma=max(a[i],max(a[i+1],a[i+2])),mi=min(a[i],min(a[i+1],a[i+2]));
        if(a[i]!=a[i+1] && a[i]!=a[i+2] && a[i+1]!=a[i+2] && (a[i+1]==ma || a[i+1]==mi)) continue;
        f[i]=true;f[i+1]=true;f[i+2]=true;
        ng.push_back(i);
        //cout << i << " ";
    }
    //cout << "" << endl;
    if(ng.size()>6){
        cout << "No" << endl;
        return;
    }
    vector<int> c={};rep(i,n) if(f[i]) c.push_back(i);
    rep(s,n){
        for(int t:c){
            swap(a[s],a[t]);
            bool ok=true;
            rep(i,3){
                if(s-i>=0 && s-i+2<n) {
                    int ma=max(a[s-i],max(a[s-i+1],a[s-i+2])),mi=min(a[s-i],min(a[s-i+1],a[s-i+2]));
                    if(a[s-i]==a[s-i+1] || a[s-i]==a[s-i+2] || a[s-i+1]==a[s-i+2]) ok=false;
                    if(a[s-i+1]!=ma && a[s-i+1]!=mi) ok=false;
                }
                if(t-i>=0 && t-i+2<n) {
                    int ma=max(a[t-i],max(a[t-i+1],a[t-i+2])),mi=min(a[t-i],min(a[t-i+1],a[t-i+2]));
                    if(a[t-i]==a[t-i+1] || a[t-i]==a[t-i+2] || a[t-i+1]==a[t-i+2]) ok=false;
                    if(a[t-i+1]!=ma && a[t-i+1]!=mi) ok=false;
                }
            }
            if(!ok){
                swap(a[s],a[t]);
                continue;
            }
            for(int s:ng){
                int ma=max(a[s],max(a[s+1],a[s+2])),mi=min(a[s],min(a[s+1],a[s+2]));
                if(a[s]==a[s+1] || a[s]==a[s+2] || a[s+1]==a[s+2]) ok=false;
                if(a[s+1]!=ma && a[s+1]!=mi) ok=false;
            }
            if(ok){
                cout << "Yes" << endl;
                return;
            }
            swap(a[s],a[t]);
        }
    }
    cout << "No" << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    int t;cin >> t;
    rep(i,t) solve();
}
0