题目描述

We have a square grid with H rows and W columns. Snuke wants to write 0 or 1 in each of the squares. Here, all of the following conditions have to be satisfied:

·For every row, the smaller of the following is A: the number of 0s contained in the row, and the number of 1s contained in the row. (If these two numbers are equal, “the smaller” should be read as “either”.)
·For every column, the smaller of the following is B: the number of 0s contained in the column, and the number of 1s contained in the column.
Determine if these conditions can be satisfied by writing 0 or 1 in each of the squares. If the answer is yes, show one way to fill the squares so that the conditions are satisfied.

Constraints
·1≤H,W≤1000
·0≤A
·2×A≤W
·0≤B
·2×B≤H
·All values in input are integers.

输入

Input is given from Standard Input in the following format:
H W A B

输出

If the conditions cannot be satisfied by writing 0 or 1 in each of the squares, print −1.

If the conditions can be satisfied, print one way to fill the squares so that the conditions are satisfied, in the following format:

s11s12⋯s1W
s21s22⋯s2W

sH1sH2⋯sHW

Here sij is the digit written in the square at the i-th row from the top and the j-th column from the left in the grid.
If multiple solutions exist, printing any of them will be accepted.

样例输入

3 3 1 1

样例输出

100
010
001

提示

Every row contains two 0s and one 1, so the first condition is satisfied. Also, every column contains two 0s and one 1, so the second condition is satisfied.


题目大意:给定一个n*m的01矩阵,规定a为每行中0或1的数目较小的一个,b为每列中0或1数目较小的一个,现在给出n,m,a,b,问是否存在一个矩阵可以满足上述规定,若不满足输出-1

题目分析:一道很好的构造题,简单却又不失难度,简单是因为zx学长做了一会就做出来了,难是因为我不会。。

实际上构造一个由四个部分组成的矩阵即可,比如这样:n=3,m=5,a=2,b=1

11000

00111

00111

大概就是这样的一个形式,就能满足所有的条件了

代码:

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<deque>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e3+100;int maze[N][N];int main()
{
//  freopen("input.txt","r",stdin);int n,m,a,b;scanf("%d%d%d%d",&n,&m,&a,&b);for(int i=1;i<=b;i++)for(int j=1;j<=a;j++)maze[i][j]=1;for(int i=b+1;i<=n;i++)for(int j=a+1;j<=m;j++)maze[i][j]=1;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)printf("%d",maze[i][j]);printf("\n");}return 0;
}

中石油训练赛 - 01 Matrix(构造)相关推荐

  1. 中石油训练赛 - Spiral Matrix(找规律)

    题目链接:点击查看 题目大意:给出一个 n * m 的矩阵,起点及初始方向可以任意选择,每次可以向前或向右走一步,问有多少种方案可以遍历所有 n * m 个格子 题目分析:找规律的题..但被数据范围误 ...

  2. 中石油训练赛 - Insertion Order(二叉搜索树+构造)

    题目大意:构造出一个长度为 n 的排列,使得按照这个顺序构造出的二叉搜索树的高度为 k 题目分析:知道 n 的大小后不难算出其可以构造的二叉搜索树高度的可行范围,下限是一棵满二叉树,这个利用倍增很快就 ...

  3. 中石油训练赛 - Edit Distance(思维+构造)

    题目描述 A binary string is a non-empty sequence of 0's and 1's, e.g., 010110, 1, 11101, etc. The edit d ...

  4. 中石油训练赛 - Trading Cards(最大权闭合子图)

    题目大意:给出 n 个卡片,可以自由买卖,且价格都是相同的,再给出 m 个集合,如果已经得到了其中一个集合中的卡片,那么可以获得该集合的收益,问如何操作可以使得收益最大化 题目分析:最大权闭合子图的模 ...

  5. 中石油训练赛 - Watch Later(状压dp)

    题目链接:点击查看 题目大意: 给出一个长度为 n 的字符串,字符串中共有 k 种不同的字符,现在问删除掉所有字符的最小操作数,对于每种字符需要确定一个先后顺序,每次需要删除掉当前所有的这种字符才能去 ...

  6. 中石油训练赛 - One-Way Conveyors(边双缩点+树上差分)

    题目链接:点击查看 题目大意:给出一张 n 个点 m 条边的无向图,现在需要将这张图转换为有向图,并且使得 k 个可达条件成立,输出一种构造方案 题目分析:如果在无向图中出现环的话,那么在转换为有向图 ...

  7. 中石油训练赛 - Swapity Swap(矩阵快速幂)

    题目描述 Farmer John's N cows (1≤N≤100) are standing in a line. The ith cow from the left has label i, f ...

  8. 中石油训练赛 - Block(二维前缀和+思维)

    题目描述 Alice得到了一张由n×m个黑白像素点组成的图片,她想要压缩这张图片.压缩图片的过程如下: 1.首先,选择一个正整数k(k>1),将图片划分成若干个k×k的小块.如果n,m不能被k整 ...

  9. 中石油训练赛 - 位置(模拟+思维)

    题目描述 由于晨晨还没有研究出核心算法,在游戏中总是被明明击败.晨晨拿出了杀手锏进行反击,精心设计了一个大型取数字求位置的难题:N*N( N是奇数)个地砖,每个上面写有一个编号,这些编号正好是1到N平 ...

最新文章

  1. Python 笔试面试及常用技巧 (1)
  2. Webwork 学习之路(四)Configuration 详解
  3. 揭开对机器学习的七点误解
  4. CodeForces - 1252L Road Construction(基环树+有源汇有上下界的最大流)
  5. python第三方包是什么意思_安装Python和第三方包的方法
  6. 对于Ping的过程,你真的了解吗?
  7. 运用xlib进行事件响应(X11 API)的小例子
  8. php的array_walk,PHP array_walk() 函数详解
  9. Android SVG 和 VectorDrawable 的基本知识
  10. 彻底理解 Android 中的阴影 1
  11. AI 假冒老板骗取 173 万!
  12. iec104点号_IEC104规约详细讲解全解.ppt
  13. 「leetcode」452. 用最少数量的箭引爆气球【贪心算法】详细图解
  14. [转].net中的认证(authentication)与授权(authorization)
  15. matlab:图像的余弦变换(DCT)
  16. 淘淘商城系列——Redis的安装
  17. win7西捷硬盘测试软件,Seagate希捷SeaTools硬盘检测工具
  18. linux heartbeat rpm,linux之heartbeat高可用的简单配置
  19. deepin配置ssh访问
  20. 深入浅出Mybatis-分页

热门文章

  1. MySQL高级 - like模糊匹配
  2. MySQL 高级 loop循环
  3. QuorumPeerMain.runFromConfig
  4. BeanDefinitionReaderUtils 向容器注册Bean
  5. 字符流读数据的2种方式
  6. IO概述(概念分类)
  7. Shiro结合redis的统一会话管理:自定义会话管理器
  8. 企业微服务:实现crud操作之dao接口
  9. 通过Zuul上传文件,禁用Zuul的Filters
  10. 密钥交换算法 - Java加密与安全