Luogu

codeforces

前言

模拟赛原题。。

好好一道送分被我硬打成爆蛋。。

\(\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的更多相关文章

  1. 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 ...

  2. Maximal Binary Matrix CodeForces - 803A (贪心+实现)

    题目链接 题意有点坑: 给你一个N*N的矩阵,让你填入K个1,使之整个矩阵关于左上到右下的对角线对称,并且这个要求这个矩阵的字典序最大. 对矩阵的字典序的定义是从每一行的第一个元素开始比较,大着为字典 ...

  3. CodeForces 803A Maximal Binary Matrix

    枚举. 枚举对角线上放多少个$1$,剩余的贪心放,更新答案. #include <iostream> #include <cstdio> #include <cstrin ...

  4. A.Kaw矩阵代数初步学习笔记 3. Binary Matrix Operations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  5. 【题解】Sonya and Matrix Beauty [Codeforces1080E]

    [题解]Sonya and Matrix Beauty [Codeforces1080E] 传送门:\(Sonya\) \(and\) \(Matrix\) \(Beauty\) \([CF1080E ...

  6. 【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 ...

  7. 【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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. 元数据治理利器 - Apache Atlas

    一.功能简介 Atlas 是一组可扩展的核心基础治理服务,使企业能够高效地满足其在 Hadoop 中的合规性要求,并允许与整个企业数据生态系统集成.Apache Atlas 为组织提供开放的元数据管理 ...

  2. C#/VB.NET 创建PDF/UA文件

    1.什么是PDF/UA文件 PDF/UA,即Universally Accessible PDF,该格式的PDF文件是于2012年8月以ISO标准14289-1发布的.具有普遍可访问的PDF文档标准. ...

  3. Atcoder ZONe Energy Programming Contest C - MAD TEAM(二分)

    文章目录 题面 Sample Input Sample output 题解 CODE 别的做法 暴力 Dynamic Programming 题面 你想从 N N N 个候选人中选 3 个人. 每个人 ...

  4. 「题解报告」P3354

    P3354 题解 题目传送门 一道很恶心的树形dp 但是我喜欢 题目大意: 一片海旁边有一条树状的河,入海口有一个大伐木场,每条河的分叉处都有村庄.建了伐木场的村庄可以直接处理木料,否则要往下游的伐木 ...

  5. Golang 动态脚本调研

    一.技术背景 1.1 程序的动态链接技术 在实际开发过程中,我们经常需要动态地更新程序的功能,或者在不变更程序主体文件的情况下添加或者更新程序模块. 1.1.1 动态链接库 首先最常见的是window ...

  6. alter role 导致的数据库无法登录问题

    ALTER ROLE  用于更改一个数据库角色.只要改角色后续开始一个新会话,指定的值将会成为该会话的默认值,并且会覆盖 kingbase.conf中存在的值或者从命令行收到的值. 显性的更改角色的一 ...

  7. maven执行跳过测试

    -Dmaven.test.skip=true 例子 mvn clean install -Dmaven.test.skip=true

  8. systemctl_用法总结

    查看开机启动项 //查询开机启动项 systemctl list-unit-files // 输出 UNIT FILE 对应服务名:STATE 是状态:enable 是开机启动,disable是开机不 ...

  9. Flink SQL 子图复用逻辑分析

    子图复用优化是为了找到SQL执行计划中重复的节点,将其复用,避免这部分重复计算的逻辑.先回顾SQL执行的主要流程 parser -> validate -> logical optimiz ...

  10. TortoiseSVN 执行清理( cleanUp )失败的解决方案

    TortoiseSVN 执行清理( cleanUp )失败的解决方案 今天碰到了一个比较棘手的问题,在这里做一下记录,以方便自己和有需要的朋友在之后碰到该类问题时有个参考. 现象 更新SVN时弹出清理 ...