Maximal Binary Matrix CodeForces - 803A (贪心+实现)
题意有点坑:
给你一个N*N的矩阵,让你填入K个1,使之整个矩阵关于左上到右下的对角线对称,并且这个要求这个矩阵的字典序最大。
对矩阵的字典序的定义是从每一行的第一个元素开始比较,大着为字典序较大。
思路:
根据字典序的定义贪心的从第一个元素开始走,如果没被填1,就填1并且关于对角线的对称的位置也填1,共计消耗两个k。
如果是i==j,即对角线的位置只需要消耗一个k。
我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,k;
int a[][];
int main()
{
cin>>n>>k;
if(n*n<k)
{
printf("-1\n"); }else
{
// for(int i=1;i<=n;i++)
// {
// if(k>0)
// {
// k--;
// a[i][i]=1;
// }
// }
repd(i,,n)
{
repd(j,,n)
{
if(a[i][j]==)
{
if(i==j)
{
if(k>)
{
k--;
a[i][j]=;
}
continue;
}
if(k>)
{
a[i][j]=;
a[j][i]=;
k-=;
}
}
}
}
repd(i,,n)
{
repd(j,,n)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
} }
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Maximal Binary Matrix CodeForces - 803A (贪心+实现)的更多相关文章
- Educational Codeforces Round 20 A. Maximal Binary Matrix
A. Maximal Binary Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 803A Maximal Binary Matrix
枚举. 枚举对角线上放多少个$1$,剩余的贪心放,更新答案. #include <iostream> #include <cstdio> #include <cstrin ...
- 题解 CF803A Maximal Binary Matrix
Luogu codeforces 前言 模拟赛原题.. 好好一道送分被我硬打成爆蛋.. \(\sf{Solution}\) 看了一波数据范围,感觉能 dfs 骗分. 骗成正解了. 大概就是将这个 \( ...
- A.Kaw矩阵代数初步学习笔记 3. Binary Matrix Operations
“矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...
- Maximal Area Quadrilateral CodeForces - 340B || 三点坐标求三角形面积
Maximal Area Quadrilateral CodeForces - 340B 三点坐标求三角形面积(可以带正负,表示向量/点的不同相对位置): http://www.cnblogs.com ...
- 【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
题目如下: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the ...
- 【leetcode】1253. Reconstruct a 2-Row Binary Matrix
题目如下: Given the following details of a matrix with n columns and 2 rows : The matrix is a binary mat ...
- Jamie and Binary Sequence (changed after round) CodeForces - 916B (贪心)
链接 大意: 求将n划分为k个2的幂的和, 且最大幂最小,字典序尽量大 比较简单的贪心练习题, 但放在div2的B题感觉偏难了..... 先只考虑最大幂最小, 首先注意到直接按n的二进制划分即可得到最 ...
- Codeforces 884E E. Binary Matrix
题 OvO http://codeforces.com/contest/884/problem/E 884e 解 考虑并查集,每个点向上方和左方的点合并,答案即为1的总数减去需要合并的次数 由于只有1 ...
随机推荐
- C#接口的显隐实现
显示接口实现与隐式接口实现 何为显式接口实现.隐式接口实现?简单概括,使用接口名作为方法名的前缀,这称为“显式接口实现”:传统的实现方式,称为“隐式接口实现”.下面给个例子. IChineseGree ...
- BookStrap之模板继承
模板继承 (extend) Django模版引擎中最强大也是最复杂的部分就是模版继承了.模版继承可以让您创建一个基本的“骨架”模版,它包含您站点中的全部元素,并且可以定义能够被子模版覆盖的 block ...
- 关于一台机器部署多个tomcat的小记
一台机器部署多个tomcat在很多时候都是有可能的,比如说多个tomcat配合nginx负载更可能好的利用CPU,或者更新程序时做主备切换等. 1.直接下载或者复制一个已有的tomcat,第一个tom ...
- Discrete Logging ZOJ - 1898 (模板题大小步算法)
就是求Ax三B(mod C)当C为素数时 #include<cstdio> #include<cstring> #include<cmath> #include&l ...
- Choosing number ZOJ - 3690 (矩阵快速幂)
题意:n个人站成一排,每个人任意从1——m中任意取一个数,要求相邻两个人的如果数字相同,数字要大于k. 分划思想推导表达式: 假设 i 个人时.第i个人的选择有两种一种是选择小于等于k的数,另一种 ...
- js 格式为2018-08-25 11:46:29 的日期比较方法
//判断日期,时间大小 function compareTime(startDate, endDate) { if (startDate.length > 0 && endDat ...
- leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree
104: class Solution { public: int maxDepth(TreeNode* root) { if(root == NULL) ; int left = maxDepth( ...
- 理解WebSocket心跳及重连机制(五)
理解WebSocket心跳及重连机制 在使用websocket的过程中,有时候会遇到网络断开的情况,但是在网络断开的时候服务器端并没有触发onclose的事件.这样会有:服务器会继续向客户端发送多余的 ...
- PAT A1107 Social Clusters (30 分)——并查集
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 15-(基础入门篇)GPRS(Air202)GPIO控制点亮一个灯
https://www.cnblogs.com/yangfengwu/p/9967027.html 现在点亮它,说一下哈,都过去好久了,不知道大家都在用哪个版本的库 http://www.openlu ...