zoukankan      html  css  js  c++  java
  • [CF803A] Maximal Binary Matrix(构造)

    题目链接:http://codeforces.com/contest/803/problem/A

    题意:n*n的矩阵,可以写k个1,问字典序最大的对称阵。

    直接从左上到右下插,遇到对角线则k--,否则都是k-=2,注意-=2的时候判断k是否>=2。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn = 110;
     5 int n, k;
     6 int G[maxn][maxn];
     7 
     8 int main() {
     9     // freopen("in", "r", stdin);
    10     // freopen("out", "w", stdout);
    11     while(~scanf("%d%d",&n,&k)) {
    12         if(k > n * n) {
    13             puts("-1");
    14             continue;
    15         }
    16         memset(G, 0, sizeof(G));
    17         for(int i = 1; i <= n; i++) {
    18             if(!k) break;
    19             for(int j = i; j <= n; j++) {
    20                 if(!k) break;
    21                 if(i == j) {
    22                     G[i][j] = 1;
    23                     k--;
    24                 }
    25                 else {
    26                     if(k >= 2) {
    27                         G[i][j] = G[j][i] = 1;
    28                         k -= 2;
    29                     }
    30                 }
    31             }
    32         }
    33         for(int i = 1; i <= n; i++) {
    34             for(int j = 1; j <= n; j++) {
    35                 printf("%d ", G[i][j]);
    36             }
    37             printf("
    ");
    38         }
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    SSM环境搭建
    spring 开发 Tars
    Tars --- Hello World
    Java IO流
    Java集合
    常用类
    Pwn With longjmp
    格式化字符串漏洞利用实战之 njctf-decoder
    一步一步 Pwn RouterOS之ctf题练手
    srop实战
  • 原文地址:https://www.cnblogs.com/kirai/p/6837858.html
Copyright © 2011-2022 走看看