题目大意:
解题思路:
首先我们看他们的定义:
b
i
b_i
bi是
a
1
,
a
2
,
a
3
,
.
.
.
.
,
a
i
,
a
i
+
1
,
.
.
.
.
.
.
.
a
2
i
−
1
a_1,a_2,a_3,....,a_i,a_{i+1},.......a_{2i-1}
a1,a2,a3,....,ai,ai+1,.......a2i−1
b
i
+
1
b_{i+1}
bi+1是
a
1
,
a
2
,
a
3
,
.
.
.
.
,
a
i
,
a
i
+
1
,
.
.
.
.
.
.
.
a
2
i
+
1
a_1,a_2,a_3,....,a_i,a_{i+1},.......a_{2i+1}
a1,a2,a3,....,ai,ai+1,.......a2i+1
我们发现
b
i
+
1
b_{i+1}
bi+1比
b
i
b_i
bi只多了
a
2
i
,
a
2
i
+
1
a_{2i},a_{2i+1}
a2i,a2i+1
那么就每次加入两个数!
那么我们看一下每一次中位数会怎么变换?
假设
c
1
,
c
2
,
c
3
.
.
.
.
.
c
i
,
c
i
+
1
,
.
.
.
.
.
.
c
2
i
−
1
c_1,c_2,c_3.....c_i,c_{i+1},......c_{2i-1}
c1,c2,c3.....ci,ci+1,......c2i−1是
a
1
,
a
2
,
a
3
,
.
.
.
.
,
a
i
,
a
i
+
1
,
.
.
.
.
.
.
.
a
2
i
−
1
a_1,a_2,a_3,....,a_i,a_{i+1},.......a_{2i-1}
a1,a2,a3,....,ai,ai+1,.......a2i−1排好序的结果
那么
b
i
=
c
i
中
位
数
b_i=c_i中位数
bi=ci中位数
对于 a 2 i , a 2 i + 1 a_{2i},a_{2i+1} a2i,a2i+1有三种情况
- 它们分别是在 c i c_i ci两端那么中位数不变
- 它们有两个都在左边,那么中位数会往右边移动一位!
- 它们有两个都在右边,那么中位数会往左边移动一位!
那么对于相邻的两个中位数
b
i
和
b
i
+
1
b_i和b_{i+1}
bi和bi+1是没有数在它们之间,因为每次只移动一位!
这个可以用树状数组去维护,不过要先离散化
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
const int N = 2e6 + 10, mod = 1e9 + 9;
const int maxn = 500010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args)
{
read(first);
read(args...);
}
int tr[maxn];
int arr[maxn];
vector<int> lis;
map<int,int> mp;
inline void add(int pos, int val) {
while(pos < maxn) {
tr[pos] += val;
pos += lowbit(pos);
}
}
inline int sum(int x) {
int res = 0;
while(x) {
res += tr[x];
x -=lowbit(x);
}
return res;
}
int main() {
IOS;
int T;
cin >> T;
while(T --) {
int n;
cin >> n;
for(int i = 1; i <= n; ++ i) {
cin >> arr[i];
lis.push_back(arr[i]);
}
sort(lis.begin(),lis.end());
lis.erase(unique(lis.begin(),lis.end()),lis.end());
for(int i = 1; i <= n; ++ i)
arr[i] = lower_bound(lis.begin(),lis.end(),arr[i]) - lis.begin() + 1;
bool flag = 1;
for(int i = 1; i <= n; ++ i) {
if(!mp.count(arr[i])) {
mp[arr[i]] = true;
add(arr[i],1);
}
if(i == 1) continue;
int x = sum(arr[i]);
int y = sum(arr[i-1]);
if(max(x,y) - min(x,y) > 1) {
flag = 0;
break;
}
}
if(flag) cout << "YES\n";
else cout << "NO\n";
for(auto it : mp) add(it.first,-1);
mp.clear();
lis.clear();
}
}