Educational Codeforces Round 20 Maximal Binary Matrix

来源:互联网 发布:淘宝店家联系方式 编辑:程序博客网 时间:2024/06/07 13:50
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
2 1
output
1 0 0 0 
input
3 2
output
1 0 0 0 1 0 0 0 0 
input
2 5
output
-1




题意 :在一个空矩阵中填 1 必须沿着左上到右下的对角线对称。。且矩阵的字典序最大。
所以优先填 i 小的 。。

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int main(){    int n,k;    while(scanf("%d%d",&n,&k)==2)    {        int a[103][103]= {0};        for(int i=0; i<n; i++)        {            if(k>0)                {a[i][i]=1;            k--;            }if(k==0)break;            for(int j=i+1; j<n; j++)            {                if(k>1)                {                    a[i][j]=1;                    a[j][i]=1;                    k-=2;                }                else break;                if(k==0)break;            }        }        if(k!=0)            printf("-1\n");        else for(int i=0; i<n; i++)                for(int j=0; j<n; j++)                {                    printf("%d",a[i][j]);                    if(j==n-1)                        printf("\n");                    else printf(" ");                }    }}


0 0
原创粉丝点击