P13778 「o.OI R2」=+#-
Genius_Star · · 题解
思路:
先转化一下题意,设
于是本质上,可以看作在一个数轴考虑,如果当前在位置
继续分析一下,考虑
于是对于
然后枚举
于是
设
于是枚举
预处理前缀和就可以算出来了,时间复杂度为
完整代码:
#include<bits/stdc++.h>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
#define popcnt(x) __builtin_popcount(x)
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e6 + 10;
inline ll read(){
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
inline void write(ll x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
int T, n, k, ans, m = 1e6;
int cnt[N], s[N], pre[N];
inline void solve(){
ans = 0;
for(int i = 0; i <= m; ++i)
cnt[i] = s[i] = pre[i] = 0;
n = read(), k = read();
for(int x, i = 1; i <= n; ++i){
x = read();
++cnt[x];
}
for(int i = 1; i <= m; ++i)
s[i] = s[i - 1] + cnt[i];
int R = k;
if(!cnt[R])
--R;
else{
while(cnt[R + 1])
++R;
}
for(int i = 1; i <= m; ++i){
if(!cnt[i])
continue;
pre[i] = cnt[i - 1] ? pre[i - 1] : i;
}
for(int k_ = 1; k_ <= R; ++k_){
if(!cnt[k_])
continue;
int l = pre[k_], r = R;
ans = max(ans, min(s[r] - s[l - 1], (n - k + k_ + 1) >> 1));
}
write(ans);
putchar('\n');
}
int main(){
T = read();
while(T--)
solve();
return 0;
}