Educational Codeforces Round 20 A. Maximal Binary Matrix

原创 2018年04月17日 17:59:19

1.题目链接

http://codeforces.com/contest/803/problem/A

2.题面

A. Maximal Binary Matrix
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given matrix with n rows and n columns filled with zeroes. You should put k ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the bottom right corner) and is lexicographically maximal.

One matrix is lexicographically greater than the other if the first different number in the first different row from the top in the first matrix is greater than the corresponding number in the second one.

If there exists no such matrix then output -1.

Input

The first line consists of two numbers n and k (1 ≤ n ≤ 1000 ≤ k ≤ 106).

Output

If the answer exists then output resulting matrix. Otherwise output -1.

Examples
input
Copy
2 1
output
Copy
1 0 
0 0 
input
Copy
3 2
output
Copy
1 0 0 
0 1 0 
0 0 0 
input
Copy
2 5
output
Copy
-1

3.题意

就是给你一个n*n的矩阵,让你填充其中的k个点,使这个矩阵满足关于对角线对称并且字典序最大。1<=n<=100,0<=k<=1e6.

4.思路

水题,可以想到字典序最大,就是按照先行后列的原则,所以直接采取双重for循环,如果当前点在对角线上,直接填充,如果不在,判断当前k是否大于2,每次填充之后更新k,当k==0退出循环。

5.代码实现

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include <time.h>
#include<string.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

const int maxn = 105;
const int Mod=1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e=2.718281828459045235360287471352662;
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
int a[maxn][maxn];
int main()
{
    ios::sync_with_stdio(false);
    int n,k;
    cin>>n>>k;
    if(k>n*n)
    {
        cout<<-1<<endl;
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        if(k==0)
            break;
        for(int j=0;j<n;j++)
        {
            if(k==0)
                break;
            if(a[i][j]==0)
            {
                if(i==j)
                {
                    a[i][j]=1;
                    k--;
                }
                else
                {
                    if(k>=2)
                    {
                        a[i][j]=1;
                        a[j][i]=1;
                        k-=2;
                    }
                }
            }
            if(k==0)
                break;
        }
        if(k==0)
            break;
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

6.反思

对于k最初等于0的条件没有考虑。以后每个if判断是否满足条件,不要在之后判断。

20小时掌握Python语言

用最短的时间,最有趣的案例,最黄最暴力的方式,带你掌握Python语言
  • 2018年02月06日 18:54

Codeforces Educational 38 C. Constructing Tests ( 构造

题目描述 Let’s denote a m-free matrix as a binary (that is, consisting of only 1’s and 0’s) matrix such...
  • wang2332
  • wang2332
  • 2018-02-18 03:29:17
  • 170

Codeforces Educational Codeforces Round 42 (Rated for Div. 2) A - Equator 二分(注意向上取整)

点击打开链接注意向上取整,因为这个问题被别人hack掉#include &amp;lt;iostream&amp;gt; #include&amp;lt;cstdio&amp;gt; #include...
  • m0_37428263
  • m0_37428263
  • 2018-04-11 09:58:18
  • 32

【解题报告】 Educational Codeforces Round 40

题目链接 A. Diagonal Walking(Codeforces 954A) 题意 给出一个仅含RRR和UUU的字符串。问如何进行一系列操作(每次操作可以将连续的RURURU或...
  • TRiddle
  • TRiddle
  • 2018-03-24 01:34:18
  • 126

Educational Codeforces Round 20 Maximal Binary Matrix

A. Maximal Binary Matrix time limit per test 1 second memory limit per test 256 megabyte...
  • qq_30591245
  • qq_30591245
  • 2017-04-29 12:51:24
  • 99

解题报告:Educational Codeforces Round 24 D,E,F

D、题目链接 题意: 给定一个长度为n的序列,给定一个数a,询问是否存在数b满足从前往后遍历序列,数b出现的次数始终大于等于数a出现的次数。 思路: 把每两个数a之间看做一个筛选阶段,每次筛选掉之...
  • qq_32570675
  • qq_32570675
  • 2017-07-01 14:07:43
  • 287

Educational Codeforces Round 42 E 贪心

题目链接 题意: 数轴上有三种类型的点:B型点、R型点、P型点。 你可以连接任意两个点,连接的代价是两个点的距离,即坐标之差。 而最终希望达到的条件是: 如果去除所有B型点,R型点和P型...
  • WuBaizhe
  • WuBaizhe
  • 2018-04-11 11:57:41
  • 56

Educational Codeforces Round 36 (Rated for Div. 2) 题解

先总结一波 第一次打cf,感觉还不错,题目做得挺顺手。虽然开始30min才想起来有这么个比赛来着。。 纪念一下第一次的rank,话说题真是水 这是大概还剩下5min的时候截的,实际可能会...
  • jpwang8
  • jpwang8
  • 2018-01-13 23:16:24
  • 370
收藏助手
不良信息举报
您举报文章:Educational Codeforces Round 20 A. Maximal Binary Matrix
举报原因:
原因补充:

(最多只允许输入30个字)