题目
题意:
对于每一个 m m m,我们都有 0 0 0~ m − 1 m-1 m−1个数字,现在你需要去掉题目给出的 n n n个数后,最后剩下的数中的中位数是多少。
思路:
中位数移动一位的情况有三种:
- 第一种就是左边增加/减少两个的时候。
- 第二种就是右边增加/减少两个的时候
- 第三种就是右边增加/减少一个且左边减少/增加一个的时候。
我们可以发现第三种在本题是最好实现的,我们可以先假设全部的值都是在中位数右边的,那么中位数 k = ( 2 m − 1 − n ) / 2 k=(2^m-1-n)/2 k=(2m−1−n)/2,然后如果出现一个 a i ≤ k a_i\leq k ai≤k的情况,那么就是第三种情况中的左增加,右减少的情况,那么中位数就要移位了,因为我们初始的时候就是把全部都在右边,这个时候左边增加了一个,总数不变,那么右边是不是就要一个减少一个,这样就符合条件了,直到出现 a i > k a_i > k ai>k的情况停止,因为 k k k是不断变化的,所以在操作之前先要排序。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1:1;
ret = (c == '-') ? 0:(c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return ;
}
inline void out(int x) {
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
inline void debug(ll x) {cout << x << endl;}
inline void debugs(string s) {cout << s << endl;}
const int maxn = 1e5 + 10;
char s[maxn];
ll a[maxn], b[maxn];
int main() {
int t;
read(t);
while (t--) {
memset(a, 0, sizeof(a));
int n, m;
read(n), read(m);
for (int i = 0; i < n; i++) {
scanf("%s", s);
int len = strlen(s);
for (int j = 0; j < len; j++) {
if (s[j] == '1') a[i] += (1ll << (len - j - 1));
}
}
sort(a, a + n);
ll k = ((1ll << m) - n - 1) >> 1;
for (int i = 0; i < n; i++) {
if (a[i] > k) break;
else k++;
}
for (int i = 0; i < m; i++) {
b[i] = (k & 1);
k >>= 1;
}
for (int i = m - 1; i >= 0; i--) out(b[i]);
putchar('\n');
}
return 0;
}