题解 CF803A Maximal Binary Matrix
前言
模拟赛原题。。
好好一道送分被我硬打成爆蛋。。
\(\sf{Solution}\)
看了一波数据范围,感觉能 dfs 骗分。
骗成正解了。
大概就是将这个 \(n\times n\) 的矩阵全扫一遍,可以选择填 \(1\) 或不填。回溯一下就行啦。
- 位置问题
假设现在的坐标为 \((x,y)\)
若 \(x>n\) ,则结束 dfs ,比较一下矩阵字典序,较为简单不题。
若 \(y>n\) ,则 \(x→x+1,y=1\) ,即跳到下一行第一位。
对称处理 (定义 \(a_{x,y}\) 为当前搜到的地方)
- 在对角线上
只填这个地方。
- 其他
填 \(a_{x,y}\) 和 \(a_{y,x}\) 。
填 \(1\) 的话就要相应处理 \(k\) 。
- 特判
\(k=0\) ,输出全 \(0\) 矩阵。
\(\sf{Code}\)
写得有点麻烦。。
#include<iostream>
#include<cstdio>
using namespace std;
int n,k;
bool flag,m[105][105],q[105][105];
inline bool p()
{
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(m[i][j]<q[i][j])
return flag=true;
return false;
}
inline void dfs(int x,int y,int d)
{
if(y>n)
++x,y=1;
if(d==0)
{
if(p())
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
m[i][j]=q[i][j];
return ;
}
if(x>n)
return ;
if(x==y&&d>=1&&!q[x][y])
q[x][y]=1,dfs(x,y+1,d-1),q[x][y]=0;
else if(x!=y&&d>=2&&(!q[x][y]||!q[y][x]))
{
if(q[x][y]==0&&q[y][x]==0)
q[x][y]=1,q[y][x]=1,dfs(x,y+1,d-2),q[x][y]=0,q[y][x]=0;
else if(q[x][y]==1&&q[y][x]==0)
q[y][x]=1,dfs(x,y+1,d-1),q[y][x]=0;
else if(q[x][y]==0&&q[y][x]==1)
q[x][y]=1,dfs(x,y+1,d-1),q[x][y]=0;
}
else dfs(x,y+1,d);
return ;
}
signed main()
{
ios::sync_with_stdio(false);
cin>>n>>k;
if(k==0)
{
for(int i=1;i<=n;++i,cout<<"\n")
for(int j=1;j<=n;++j)
cout<<"0 ";
return 0;
}
if(k>n*n)
{
cout<<-1;
return 0;
}
q[1][1]=1;
dfs(1,2,k-1);
if(flag)
for(int i=1;i<=n;++i,cout<<"\n")
for(int j=1;j<=n;++j)
cout<<m[i][j]<<" ";
else cout<<"-1";
return 0;
}
题解 CF803A Maximal Binary Matrix的更多相关文章
- Educational Codeforces Round 20 A. Maximal Binary Matrix
A. Maximal Binary Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Maximal Binary Matrix CodeForces - 803A (贪心+实现)
题目链接 题意有点坑: 给你一个N*N的矩阵,让你填入K个1,使之整个矩阵关于左上到右下的对角线对称,并且这个要求这个矩阵的字典序最大. 对矩阵的字典序的定义是从每一行的第一个元素开始比较,大着为字典 ...
- CodeForces 803A Maximal Binary Matrix
枚举. 枚举对角线上放多少个$1$,剩余的贪心放,更新答案. #include <iostream> #include <cstdio> #include <cstrin ...
- A.Kaw矩阵代数初步学习笔记 3. Binary Matrix Operations
“矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...
- 【题解】Sonya and Matrix Beauty [Codeforces1080E]
[题解]Sonya and Matrix Beauty [Codeforces1080E] 传送门:\(Sonya\) \(and\) \(Matrix\) \(Beauty\) \([CF1080E ...
- 【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
题目如下: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the ...
- 【leetcode】1253. Reconstruct a 2-Row Binary Matrix
题目如下: Given the following details of a matrix with n columns and 2 rows : The matrix is a binary mat ...
- LeetCode 题解 Search a 2D Matrix II。巧妙!
[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30 ...
- [LeetCode 题解]: Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- Spring源码 08 IOC refresh方法3
参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...
- 客户流失?来看看大厂如何基于spark+机器学习构建千万数据规模上的用户留存模型 ⛵
作者:韩信子@ShowMeAI 大数据技术 ◉ 技能提升系列:https://www.showmeai.tech/tutorials/84 行业名企应用系列:https://www.showmeai. ...
- C#基础_C#判断文件是否被打开
1.用文件流判断 using System; using System.Collections.Generic; using System.IO; using System.Linq; using S ...
- 大家都能看得懂的源码 - 那些关于DOM的常见Hook封装(二)
本文是深入浅出 ahooks 源码系列文章的第十五篇,该系列已整理成文档-地址.觉得还不错,给个 star 支持一下哈,Thanks. 本篇接着针对关于 DOM 的各个 Hook 封装进行解读. us ...
- Homework5
问:什么是分而治之? 答:分而治之就是通过一系列的方法,将复杂的问题逐渐划分成若干份相对简单的问题的方法,对每个相对简单问题进行一一解决,若干个简单问题的解的集合就是一个复杂问题的解.分而治之的应用体 ...
- 【python】生成一段连续的日期
date-gen.py import datetime def date_generate(start_date, end_date): print(f'Hi, {start_date}, {end_ ...
- Mysql 实现 向上递归查找父节点并返回树结构
需求:通过mysql 8.0以下版本实现,一个人多角色id,一个角色对应某个节点menu_id,根节点的父节点存储为NULL, 向上递归查找父节点并返回树结构. 如果只有叶子,剔除掉; 如果只有根,只 ...
- 使用Elasticsearch Operator快速部署Elasticsearch集群
转载自:https://www.qikqiak.com/post/elastic-cloud-on-k8s/ 随着 kubernetes 的快速发展,很多应用都在往 kubernetes 上面迁移,现 ...
- 俄罗斯的 HTTPS 证书问题
文章转载自:https://mp.weixin.qq.com/s/8EikwCvZgKt2TFsld-nKSA
- Linux yum安装PostgreSQL9.6
PostgreSQL10版本的主从安装配置在 https://www.cnblogs.com/virtulreal/p/11675841.html 一.下载安装 1.创建PostgreSQL9.6的y ...