/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 536.0 KiB
#2 Accepted 6ms 1.27 MiB
#3 Accepted 2ms 536.0 KiB
#4 Accepted 8ms 580.0 KiB
#5 Wrong Answer 13ms 788.0 KiB
#6 Accepted 8ms 532.0 KiB
#7 Accepted 4ms 536.0 KiB
#8 Accepted 5ms 532.0 KiB
#9 Accepted 14ms 532.0 KiB
#10 Wrong Answer 1ms 532.0 KiB

Code

/**
 *    author:   Binoy Barman
 *    created:  2025-08-31 09:42:08
**/

#include<bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) 42
#endif
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = 2e9;

#define int long long
#define nl '\n'
#define all(v) v.begin(), v.end()
#define clg(x) (32 - __builtin_clz(x))
#define Testcase_Handler int tts, tc = 0; cin >> tts; hell: while(tc++ < tts)
#define uniq(v) sort(all(v)), v.resize(distance(v.begin(), unique(v.begin(), v.end())))
template<class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {bool first = true;for(auto &x : a) {if(!first) out << ' ';first = false;out << x;}return out;};

namespace Dark_Lord_Binoy {
inline void init() {
    ios::sync_with_stdio(false); 
    cin.tie(nullptr);

    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}
}

int32_t main() {
    Dark_Lord_Binoy::init();
    Testcase_Handler {
        int n;
        cin >> n;
        vector<int> a(n);
        cin >> a;
        if(n == 1) {
            cout << "YES" << nl;
            goto hell;
        }

        vector<int> c(a);
        sort(all(c));
        map<int, pair<int, int>> f;
        for (int i = 0; i < n; i++) {
            if(i == 0) f[c[i]] = {-1, c[i + 1]};
            else if(i == n - 1) f[c[i]] = {c[i - 1], -1};
            else f[c[i]] = {c[i - 1], c[i + 1]};
        }
        dbg(a);
        dbg(f);
        bool ok = true;
        int l = 1, r = n - 1;
        deque<int> b;
        b.push_back(a[0]);

        while(l <= r) {
            auto x = f[b.back()];
            auto y = f[b.front()];

            dbg(l, r, b, x, y);
            if(a[l] == x.second) {
                b.push_back(a[l]);
                l++;
            }
            else if(a[l] == y.first) {
                b.push_front(a[l]);
                l++;
            }
            else if(a[r] == x.second) {
                b.push_back(a[r]);
                r--;
            }
            else if(a[r] == y.first) {
                b.push_front(a[r]);
                r--;
            }
            else {
                ok = false;
                break;
            }
        }
        
        if(ok) {
            cout << "YES" << nl;
            goto hell;
        }

        ok = true;
        l = 0, r = n - 2;
        b.clear();
        b.push_back(a[n - 1]);

        while(l <= r) {
            auto x = f[b.back()];
            auto y = f[b.front()];

            dbg(l, r, b, x, y);
            if(a[l] == x.second) {
                b.push_back(a[l]);
                l++;
            }
            else if(a[l] == y.first) {
                b.push_front(a[l]);
                l++;
            }
            else if(a[r] == x.second) {
                b.push_back(a[r]);
                r--;
            }
            else if(a[r] == y.first) {
                b.push_front(a[r]);
                r--;
            }
            else {
                ok = false;
                break;
            }
        }
        cout << (ok ? "YES" : "NO") << nl;
    }

    dbg(_Time_);
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1229 Array of Beauty
Contest
Testing - Intra LU Programming contest 25
Language
C++17 (G++ 13.2.0)
Submit At
2025-08-31 05:17:35
Judged At
2025-08-31 05:17:35
Judged By
Score
80
Total Time
14ms
Peak Memory
1.27 MiB