/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 10ms 1.484 MiB
#3 Accepted 2ms 532.0 KiB
#4 Accepted 5ms 600.0 KiB
#5 Accepted 7ms 1.082 MiB
#6 Accepted 4ms 788.0 KiB
#7 Accepted 1ms 532.0 KiB
#8 Accepted 4ms 532.0 KiB
#9 Accepted 26ms 576.0 KiB
#10 Accepted 3ms 516.0 KiB
#11 Accepted 66ms 7.32 MiB
#12 Accepted 62ms 7.414 MiB

Code

#include "bits/stdc++.h"
#ifndef LOCAL
#define dbg(...)
#define debug(...)
#endif

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

using namespace std;
#define int long long
#define sz(v) (int)(v).size()
#define all(v) begin(v),end(v)
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';
#define unique(v) sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<ll>(l, r)(rng)
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
template<class T> using V = vector<T>;
// template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int op(int a, int b) {return a+b;}
struct ST {
    int n;
    vector<int> t;

    ST(int _n) {
        n = _n;
        t.assign(2 * n + 1, 0);
    }

    void upd(int p, int v) {
        p += n;
        t[p] += v;
        for (p >>= 1; p > 0; p >>= 1) t[p] = op(t[p << 1], t[p << 1 | 1]);
    }

    int query(int l, int r) {
        int res = 0;
        l += n, r += n;
        for (; l<=r; l>>=1, r>>=1) {
            if (l & 1) res = op(res, t[l++]);
            if (!(r & 1)) res = op(res, t[r--]);
        }
        return res;
    }
};

void compress(vector<int> &v){
	map<int,int> mp;
	for (auto &it:v) mp[it];
	int now=0;
	for (auto &it:mp) it.second=++now;
	for (auto &it:v) it=mp[it];
}

void shelby(int tc) {
	int n;
	cin>>n;
	vector<int> v(n);
	cinv(v)
	compress(v);
	ST st(n+5);
	for (auto &it:v) st.upd(it, 1);
	vector<int> a=v;
	sort(all(a));
	deque<int>dq{v.front()};
	st.upd(v.front(), -1);
	bool f=true;
	for (int i=1,j=n-1;i<=j;){
		int x=dq.front(), y=dq.back();
		if (v[i]<=x && st.query(v[i]+1, x)==0){
			dq.push_front(v[i]);
			st.upd(v[i], -1);
			i++;
			continue;
		}
		if (v[j]<=x && st.query(v[j]+1, x)==0){
			dq.push_front(v[j]);
			st.upd(v[j], -1);
			j--;
			continue;
		}
		if (v[i]>=y && st.query(y, v[i]-1)==0){
			dq.push_back(v[i]);
			st.upd(v[i], -1);
			i++;
			continue;
		}
		if (v[j]>=y && st.query(y, v[j]-1)==0){
			dq.push_back(v[j]);
			st.upd(v[j], -1);
			j--;
			continue;
		}
		f=false;
		break;
	}
	if (f) return void(cout<<"YES\n");


	ST st2(n+5);
	for (auto &it:v) st2.upd(it, 1);
	dq.clear();
	dq.push_back(v.back());
	st2.upd(v.back(), -1);
	f=true;
	for (int i=0,j=n-2;i<=j;){
		int x=dq.front(), y=dq.back();
		if (v[i]<=x && st2.query(v[i]+1, x)==0){
			dq.push_front(v[i]);
			st2.upd(v[i], -1);
			i++;
			continue;
		}
		if (v[j]<=x && st2.query(v[j]+1, x)==0){
			dq.push_front(v[j]);
			st2.upd(v[j], -1);
			j--;
			continue;
		}
		if (v[i]>=y && st2.query(y, v[i]-1)==0){
			dq.push_back(v[i]);
			st2.upd(v[i], -1);
			i++;
			continue;
		}
		if (v[j]>=y && st2.query(y, v[j]-1)==0){
			dq.push_back(v[j]);
			st2.upd(v[j], -1);
			j--;
			continue;
		}
		f=false;
		break;
	}
	if (f) return void(cout<<"YES\n");
	cout<<"NO\n";
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    for (int _ = 1; _ <= t; ++_) shelby(_);
}
  

Information

Submit By
Type
Submission
Problem
P1229 Array of Beauty
Language
C++17 (G++ 13.2.0)
Submit At
2025-09-05 20:28:55
Judged At
2025-09-06 13:13:12
Judged By
Score
100
Total Time
66ms
Peak Memory
7.414 MiB