题意:
给你
1
,
2
,
.
.
.
,
n
1,2,...,n
1,2,...,n一共
n
n
n个数,你需要将其分成
m
m
m组,使得每组的中位数为
b
i
b_i
bi,保证
b
i
b_i
bi互不相同。
中位数取
c
k
+
1
2
c_{\frac{k+1}{2}}
c2k+1
n
,
m
≤
1
e
5
n,m\le1e5
n,m≤1e5
思路:
考虑将中位数看作分割点,那么这个序列就被分成了
m
+
1
m+1
m+1段,我们发现每两段都可以各取一个数来抵消,所以最后剩下的一定都在同一段里。
考虑最大段中包含的个数,分两种情况讨论:
(
1
)
(1)
(1)最大段中的个数
≤
\le
≤其他各段之和,此时可以将除了最大段的其他段相互抵消,让后再让其与最大段抵消,由于每次抵消只能减去偶数个,所以最终一定只剩下了
0
0
0个或
1
1
1个,如果剩了一个我们可以让最后一个剩下,让后放在前面的某个后面即可。
(
2
)
(2)
(2)最大段的个数
>
>
>其他段之和,此时抵消后剩余的数都在这个最大段内,假设最大段之前有
x
x
x个位置是要求的中位数,那么如果
x
≥
x\ge
x≥剩余的数的时候有解。
最后排个序判断一下即可。
// Problem: E - Median
// Contest: Virtual Judge - 2021多校第六场补题
// URL: https://vjudge.net/contest/451552#problem/E
// Memory Limit: 524 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,m;
int a[N];
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int _; scanf("%d",&_);
while(_--) {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) a[i]=0;
for(int i=1;i<=m;i++) {
int x; scanf("%d",&x);
a[x]++;
}
vector<PII>v;
int cnt=0;
for(int i=1;i<=n;i++) {
int num=0;
while(i<=n&&!a[i]) num++,i++;
v.pb({num,cnt});
cnt++;
}
sort(v.begin(),v.end());
int sum=0;
for(auto x:v) sum+=x.X;
if(v.back().X<=sum-v.back().X+v.back().Y) puts("YES");
else puts("NO");
}
return 0;
}
/*
*/