题目描述:
time limit per test
2 seconds
memory limit per test
256 megabytes

Consider all binary strings of length m (1≤m≤60). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly 2m such strings in total.

The string s is lexicographically smaller than the string t (both have the same length m) if in the first position i from the left in which they differ, we have s[i]<t[i]. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second.

We remove from this set n (1≤n≤min(2m−1,100)) distinct binary strings a1,a2,…,an, each of length m. Thus, the set will have k=2m−n strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary).

We number all the strings after sorting from 0 to k−1. Print the string whose index is ⌊(k−1)/2⌋ (such an element is called median), where ⌊x⌋ is the rounding of the number down to the nearest integer.

For example, if n=3, m=3 and a=[010, 111, 001], then after removing the strings ai and sorting, the result will take the form: [000, 011, 100, 101, 110]. Thus, the desired median is 100.

Input
The first line contains an integer t (1≤t≤1000) — the number of test cases. Then, t test cases follow.

The first line of each test case contains integers n (1≤n≤min(2m−1,100)) and m (1≤m≤60), where n is the number of strings to remove, and m is the length of binary strings. The next n lines contain a1,a2,…,an — distinct binary strings of length m.

The total length of all given binary strings in all test cases in one test does not exceed 105.

Output
Print t answers to the test cases. For each test case, print a string of length m — the median of the sorted sequence of remaining strings in the corresponding test case.

Example
input

5
3 3
010
001
111
4 3
000
111
100
011
1 1
1
1 1
0
3 2
00
01
10

output

100
010
0
1
11

Note
The first test case is explained in the statement.

In the second test case, the result after removing strings and sorting is [001, 010, 101, 110]. Therefore, the desired median is 010.

算法标签:思维、构造

题目大意:给定长度为m的所有二进制串(共2^m个),根据字典序从小到大排列后,删去其中n个串(a1,a2…,an),求删去后的中位串(第⌊(k−1)/2⌋个)

首先观察m<=60,可以直接将二进制串转化为十进制数,把问题转化到一根数轴上,不需要再从字符串角度考虑。

设最终答案为med,那么要求出答案只需要知道给出的n个数中,有几个在med右侧(>med),有几个在med左侧(<med)即可。

此处有两个思维上的难点:

  1. 当我们依次将ai与med比较时,med是在动态变化的(意味着当前关于ai和med大小的判断可能有误)
  2. 删去一个数后,数列长度发生改变,其余点的中位数求起来也比较麻烦。

所以为了解决以上两个问题,可以简单构造一下。

如图,一开始,我们假设给出的n个点都在med左侧,因为是连续的,所以这个数列的性质非常清晰。设数列左端l=n,右端r=2^m-1,那么实际数列长度为(r-l+1),中位数就是(l+r)/2


但是当然ai也可能在med右侧,此时如何调整呢。之前说到,我们只关注这个ai在med左侧还是右侧,而不关注它具体是多少。所以,只要把任意两个med左侧和右侧的数互换一下就可以了。为了维护数列的连续性,我们可以交换n-1和2^m-1这两个数。

把上面两张图对比着看一下,很显然,我们只是把蓝色的一段整体左移了一个数而已,那么med也只需要减1就可以完成变换了。

最后一个问题是,med仍然是动态变化的,如何在判断每个ai“在med左侧还是右侧”的时候都能确保判断是正确的呢。

只要先将ai从大到小排序即可。(具体说是因为这种构造方式维护了med的单调递减,那么排序后如果ai>=med,ai一定大于等于最终的中位数,如果ai<med,后续比较中ai始终小于med,med不再发生变化,最终的中位数就是med)

所以这种构造法最关键的地方是维护了区间长度和连续性,对于一个固定长度并连续的区间,处理起来就非常方便。

相反,如果完全按照题意进行模拟,一些边界情况的处理可能会变得非常烧脑,且不易调试和理解。(当然,这对思维能力和码力足够的大佬来说可能并不是什么问题…)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;const int N=100+5;char s[N],ans[N];ll l,r,jc[N],A[N];bool cmp(ll a,ll b){ return a>b; }
int main()
{int T,n,m;jc[0]=1;for (int i=1;i<=60;i++) jc[i]=jc[i-1]*2;cin>>T;while(T--){cin>>n>>m;l=0,r=jc[m]-1;for (int i=1;i<=n;i++){scanf("%s",s);ll tmp=0;for (int j=0;j<m;j++){tmp=tmp*2+s[j]-'0';}A[i]=tmp;}sort(A+1,A+1+n,cmp);ll cur=(n+r)/2;for (int i=1;i<=n;i++){if (A[i]>=cur)cur--;}ans[m]='\0';for (int i=m-1;i>=0;i--){ans[i]=cur%2+'0';cur/=2;}printf("%s\n",ans);}return 0;
}

Codeforces Round #644 1360H. Binary Median(思维、构造)相关推荐

  1. Codeforces Round #644 (Div. 3) E.Polygon

    Codeforces Round #644 (Div. 3) E.Polygon 题目链接 Polygon is not only the best platform for developing p ...

  2. Codeforces Round #644 (Div. 3) G.A/B Matrix

    Codeforces Round #644 (Div. 3) G.A/B Matrix 题目链接 You are given four positive integers n, m, a, b (1≤ ...

  3. Codeforces Round #644 (Div. 3) D.Buying Shovels

    Codeforces Round #644 (Div. 3) D.Buying Shovels 题目链接 Polycarp wants to buy exactly n shovels. The sh ...

  4. Codeforces Round #644 (Div. 3) F.Spy-string

    Codeforces Round #644 (Div. 3) F.Spy-string 题目链接 You are given n strings a1,a2,-,an: all of them hav ...

  5. Codeforces Round #624 (Div. 3) E. Construct the Binary Tree 思维 + 构造

    传送门 文章目录 题意: 思路: 题意: 给你n,dn,dn,d,让你构造有nnn个点的二叉树,他们每个节点深度和为ddd. n,d≤3000n,d\le 3000n,d≤3000. 思路: 先考虑不 ...

  6. Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

    Codeforces Round #459 (Div. 2) C. The Monster 题意:定义正确的括号串,是能够全部匹配的左右括号串. 给出一个字符串,有 (.). ? 三种字符, ? 可以 ...

  7. Codeforces Round #446 (Div. 1) B. Gluttony 构造 + 补集思想

    传送门 文章目录 题意: 思路: 题意: 给你一个数组aaa,保证aaa中每个数都互不相同,让你构造一个数组bbb,满足对于任意的S=x1,x2,...,xk,1≤xi≤n,0≤k<nS={x_ ...

  8. Codeforces Round #592 (Div. 2) F. Chips 构造 + 细节

    传送门 文章目录 题意: 思路: 题意: 思路: 恶心的构造题,思路很简单但是代码细节很多,搞了半天. 根据题目的性质不难发现,如果有两个相同颜色的球相邻,那么他们的颜色永远不会改变. 根据这个性质, ...

  9. Codeforces Round #644(Div. 3) A-H

    A - Minimal Square 题意 给两个完全一样的矩形(平行且不重叠) 求能覆盖两个矩形的最小正方形的面积 思路 只有两种摆放方式 将两个矩形上下并列或者左右并列 得到的新图形 长或者宽是之 ...

最新文章

  1. STL学习系列九:Map和multimap容器
  2. 2018全球智能+新商业峰会倒计时2个月,5000名AI从业者将出席
  3. Nat. Mach. Intell. | 人工智能的透明度和可重复性
  4. slim.flatten——将输入扁平化但保留batch_size,假设第一维是batch
  5. 有关绑定没有数据显示的问题
  6. springmvc+mybatis+ajax 批量插入数据
  7. [云炬创业基础笔记]第四章测试19
  8. oracle索引机制
  9. 零基础逆向工程28_Win32_02_事件_消息_消息处理函数
  10. Android 内存溢出解决方案(OOM)
  11. 前端学习(1852)vue之电商管理系统电商系统托管码云上
  12. Spring Cloud核心组件原理分析
  13. Q79:怎么用三角形网格(Triangle Mesh)细分曲面
  14. Linux之SDL2+OpenGL+EGL绘制(十六)
  15. 10 个基于 JavaScript 的机器学习实例
  16. LMDB:闪电内存映射数据库管理器
  17. 超出本地计算机网络,超出本地计算机网络适配器卡的名称限制怎么解决?
  18. dropbox为什么被屏蔽_Python社区和Dropbox为增加多样性而采取的步骤
  19. 三大 Windows 文件搜索神器 Everything、Listary、AnyTXT Searcher 功能特色与区别详解
  20. yum -y insytall nmap报错:​​​​​​​​已加载插件:fastestmirrorLoading mirror speeds from cached hostfile

热门文章

  1. 串口/COM口、USB、RS232、RS422、RS485的区别
  2. 实例比较单精度浮点型,双精度浮点型运算结果精度
  3. IDEA中Artifact的配置
  4. 国内计算机类学术期刊投稿指南
  5. 【Latex 表格】换行+行高
  6. [蓝屏]driver_unloaded_without_cancelling_pending_operations
  7. 多项式 商环 域(群论笔记)
  8. IOS Swift语言开发 tableView的重用以及自cell的自适应高度
  9. python opencv resize函数_OpenCV尺寸调整函数resize
  10. 利用OpenGL设计贪吃蛇游戏