Educational Codeforces Round 20 A. Maximal Binary Matrix
1.题目链接
http://codeforces.com/contest/803/problem/A
2.题面
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.
The first line consists of two numbers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ 106).
If the answer exists then output resulting matrix. Otherwise output -1.
2 1
1 0 0 0
3 2
1 0 0 0 1 0 0 0 0
2 5
-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判断是否满足条件,不要在之后判断。