枚举。

枚举对角线上放多少个$1$,剩余的贪心放,更新答案。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std; int n,k;
int a[110][110];
int b[110][110]; void ok()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]!=b[i][j])
{
if(a[i][j]>b[i][j])
{
for(int ii=1;ii<=n;ii++)
{
for(int jj=1;jj<=n;jj++)
{
b[ii][jj] = a[ii][jj];
}
} }
return ;
}
}
}
} int main()
{
scanf("%d%d",&n,&k);
if(n*n<k)
{
printf("-1\n");
return 0;
} int suc = 0; for(int x=0;x<=n;x++)
{
memset(a,0,sizeof a); if(x>k) continue;
if((k-x)%2!=0) continue;
if( n*n - n < k-x ) continue; for(int i=1;i<=x;i++) a[i][i] = 1; int sum = 0;
if((k-x)!=0)
{
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
a[i][j] = a[j][i]=1;
sum+=2;
if(sum == k-x) break;
}
if(sum == k-x) break;
}
} if(sum == k-x) suc=1;
ok();
} if(suc==0) printf("-1\n");
else
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
printf("%d",b[i][j]);
if(j<n) printf(" ");
else printf("\n");
}
}
} return 0;
}

CodeForces 803A 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 884E E. Binary Matrix

    题 OvO http://codeforces.com/contest/884/problem/E 884e 解 考虑并查集,每个点向上方和左方的点合并,答案即为1的总数减去需要合并的次数 由于只有1 ...

  4. 题解 CF803A Maximal Binary Matrix

    Luogu codeforces 前言 模拟赛原题.. 好好一道送分被我硬打成爆蛋.. \(\sf{Solution}\) 看了一波数据范围,感觉能 dfs 骗分. 骗成正解了. 大概就是将这个 \( ...

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

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

  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. CodeForces 313C Ilya and Matrix

    Ilya and Matrix Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  9. codeforces 803C Maximal GCD(GCD数学)

    Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...

随机推荐

  1. Python urllib urlretrieve函数解析

    Python urllib urlretrieve函数解析 利用urllib.request.urlretrieve函数下载文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 Ur ...

  2. HDU 6061 推导 NTT

    复函数,递归代入,可以得到最终的式子为$f(x-\sum_{i=1}^{m}{a_i})$,且$f(x) = \sum_{i = 0}^{n}{c_ix^i}$,求最终各个x项的系数. 设$S=\su ...

  3. 重构改善既有代码设计--重构手法15:Remove Middle Man (移除中间人)

    某个类做了过多的简单委托动作.让客户直接调用受托类. 动机:在Hide Delegate (隐藏委托关系)的“动机”中,谈到了“封装委托对象”的好处.但是这层封装也是要付出代价的,它的代价是:每当客户 ...

  4. bootstrap下laydate样式错乱问题

    查看发现bs使用了 * {box-sizing:border-box;}重置了盒子模型 那么我们再把它重置回来,在样式中加入以下代码 .laydate_box, .laydate_box * { bo ...

  5. 【Project Euler】530 GCD of Divisors 莫比乌斯反演

    [题目]GCD of Divisors [题意]给定f(n)=Σd|n gcd(d,n/d)的前缀和F(n),n=10^15. [算法]莫比乌斯反演 [题解]参考:任之洲数论函数.pdf 这个范围显然 ...

  6. Oracle笔记之序列(Sequence)

    Oracle中序列是一种数据对象,可以视为一个等差数列,我们自增就是一个遍历这个数列的过程,可以取当前值,也可以将当前值自加n后返回,Sequence与表没有太大的关系,有的时候如果表的主键是数值类型 ...

  7. 离散化&&逆序数对

    题目:http://www.fjutacm.com/Problem.jsp?pid=3087 #include<stdio.h> #include<string.h> #inc ...

  8. Perl6多线程4: Promise allof / anyof

    allof   : 所有代码块执行完成后才退出 anyof :只要有一个代码块执行完后就马上退出 要配合 await 一起用: my $p = start {say 'a'}; ;say 'b';} ...

  9. oracle客户端不需要配置tnsnames.ora文件直接连接服务器数据库

    在以前的oracle使用过程中,想要在客户端连接到服务器时,都是在客户端中的tnsnames.ora文件配置如以下内容: adb = (DESCRIPTION = (ADDRESS_LIST = (A ...

  10. C语言俄罗斯方块

    #include <windows.h> #include <stdio.h> #include <time.h> #include <conio.h> ...