# Educational Codeforces Round 20 A - Maximal Binary Matrix

1.题目链接

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.题意解释

4.思路

5.代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 105;

int mat[maxn][maxn];

int main()
{
memset(mat, 0, sizeof(mat));

int n, k;
while (scanf("%d%d", &n, &k) == 2)
{
if (k > n * n)
printf("-1\n");
else
{
for (int i = 0; i < n; ++i)
{
if (k == 0)  //所有的1已经放完了，结束循环
break;
for (int j = 0; j < n; ++j)
{
if (k == 0)  //所有的1已经放完了，结束循环
break;
if (i == j)  //主对角线上的元素
{
mat[i][j] = 1;
--k;
}
else if (i < j)  //非主对角线上的元素
{
if (k == 1)
continue;
mat[i][j] = 1;
mat[j][i] = 1;
k -= 2;
}
}
}

for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
printf("%d", mat[i][j]);
if (j == n - 1)
printf("\n");
else
printf(" ");
}
}
}
}

return 0;
}

• 评论

• 下一篇