CF1536D - Omkar and Medians(思维)
题目
题解
对于数组B,每增加1个数,相当于数组A增加2个数。设B中上一个数为x,新加的数为y,如果y>x,那么A增加的两个均大于等于x;如果y<x,那么两个均小于等于x;如果y=x,那么一个大于等于x一个小于等于x。在链表上维护一个指针cur,它始终指向每一步的x。如果y>x,y位于x的右侧,cur右移;如果y<x,cur左移;如果相等,cur不动。期间始终保持cur指向x的性质,有一步cur不指向x,说明不可能。
详见代码
#include <bits/stdc++.h>
#define endl '\n'
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mp make_pair
#define seteps(N) fixed << setprecision(N)
typedef long long ll;
using namespace std;
/*-----------------------------------------------------------------*/
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
#define INF 0x3f3f3f3f
const int N = 3e5 + 10;
const double eps = 1e-5;
int arr[N];
int main() {
IOS;
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> arr[i];
}
list<int> l;
l.push_back(arr[1]);
auto cur = l.begin();
bool ok = true;
for(int i = 2; i <= n; i++) {
if(arr[i] > arr[i - 1]) {
if(*cur > arr[i]) {
ok = false;
break;
}
if(*cur == arr[i]) continue;
++cur;
if(cur == l.end()) {
cur = l.insert(cur, arr[i]);
} else {
if(*cur < arr[i]) {
ok = false;
break;
} else if(*cur > arr[i]) {
cur = l.insert(cur, arr[i]);
}
}
} else if(arr[i] < arr[i - 1]) {
if(*cur < arr[i]) {
ok = false;
break;
}
if(*cur == arr[i]) continue;
if(cur == l.begin()) {
cur = l.insert(cur, arr[i]);
} else {
--cur;
if(*cur > arr[i]) {
ok = false;
break;
} else if(*cur < arr[i]){
++cur;
cur = l.insert(cur, arr[i]);
}
}
}
}
if(ok) cout << "YES" << endl;
else cout << "NO" << endl;
}
}