CF1360H Binary Median 题解
提供一份好看的题解。
\(2^m-n\) 个数的中位数排名是 \(\lfloor\dfrac{2^m-n-1}2\rfloor\)(从 \(0\) 开始)。因为所有元素是连续的,只要数出被删掉的比中位数小的元素数量,那么 \(\lfloor\dfrac{2^m-n-1}2\rfloor\) 加上数量就是中位数了。
不过不知道中位数的大小,不能确定比中位数小的元素数量。可以采取二分。更简单的方式是把元素从小到大枚举。
略带麻烦的是输入数出,使用 bitset
解决很方便。
复杂度在于读入和排序,\(\mathcal O(nm+n\log n)\)。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<bitset>
using std::cin;using std::cout;
using lolu=unsigned long long;
constexpr int N=101;
int n,m;
lolu a[N];
std::bitset<64>num;
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
std::ios::sync_with_stdio(false);cin.tie(nullptr);
int T;cin>>T;while(T--){
cin>>n>>m;
lolu mid=((1ull<<m)-n-1)/2;
for(int i=1;i<=n;i++)cin>>num,a[i]=num.to_ullong();
std::sort(a+1,a+n+1);
for(int i=1;i<=n;i++)if(a[i]>mid)break;else mid++;
num=mid;
for(int i=m-1;~i;i--)cout<<num[i];
cout<<'\n';
}
return 0;
}