/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 21ms 636.0 KiB
#3 Accepted 5ms 532.0 KiB
#4 Accepted 25ms 600.0 KiB
#5 Accepted 5ms 532.0 KiB
#6 Accepted 5ms 532.0 KiB
#7 Accepted 4ms 532.0 KiB
#8 Accepted 6ms 424.0 KiB
#9 Accepted 40ms 532.0 KiB
#10 Accepted 1ms 536.0 KiB
#11 Accepted 43ms 2.031 MiB
#12 Accepted 35ms 2.02 MiB

Code

#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> a;
deque<int> temp1, temp2;

bool solve(int pos) {
    int l = pos, r = pos;
    while (!temp2.empty()) {
        int first = temp2.front();
        int second = temp2.back();
        if (l > 0 && (a[l - 1] == first || a[l - 1] == second)) {
            l--;
            if (a[l] == first) {
                temp2.pop_front();
            } else {
                temp2.pop_back();
            }
        } else if (r + 1 < n && (a[r + 1] == first || a[r + 1] == second)) {
            r++;
            if (a[r] == first){
                temp2.pop_front();
            } else {
                temp2.pop_back();
            }
        } else {
            return false;
        }
    }
    return true;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        bool ok = false;
        cin >> n;
        vector<int> A(n);
        for (int i = 0; i < n; i++) {
            cin >> A[i];
            temp1.push_back(A[i]);
        }
        temp2 = temp1;
        a = A;
        sort(a.begin(), a.end());
        for (int i = 0; i < n; i++) {
            if (a[i] == temp2.front()) {
                temp2.pop_front();
                ok = solve(i);
                break;
            }
        }
        while (!temp2.empty()){
            temp2.pop_back();
        }
        temp2 = temp1;
        for (int i = 0; i < n; i++) {
            if (a[i] == temp2.back()) {
                temp2.pop_back();
                ok = ok || solve(i);
                break;
            }
        }
        cout << (ok ? "YES " : "NO") << endl;
        while (!temp2.empty()){
            temp2.pop_back();
        }
        while (!temp1.empty()){
            temp1.pop_back();
        }
    }
}

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 16:36:33
Judged At
2025-08-31 16:36:33
Judged By
Score
100
Total Time
43ms
Peak Memory
2.031 MiB