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=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ int n,k; int a[1000][1000]; 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,1,n){repd(j,1,n){if(a[i][j]==0){if(i==j){if(k>0){k--;a[i][j]=1;}continue;}if(k>1){a[i][j]=1;a[j][i]=1;k-=2;}}}}repd(i,1,n){repd(j,1,n){cout<<a[i][j]<<" ";}cout<<endl;}}return 0; }inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }
转载于:https://www.cnblogs.com/qieqiemin/p/10240176.html
Maximal Binary Matrix CodeForces - 803A (贪心+实现)相关推荐
- codeforces 884E Binary Matrix 并查集,滚动数组
E. Binary Matrix time limit per test 3 seconds memory limit per test 16 megabytes input standard inp ...
- Codeforces 884E E. Binary Matrix
题 OvO http://codeforces.com/contest/884/problem/E 884e 解 考虑并查集,每个点向上方和左方的点合并,答案即为1的总数减去需要合并的次数 由于只有1 ...
- CF- Educational Codeforces Round 97 (Rated for Div. 2)-1437B. Reverse Binary Strings【思维/贪心】
题目链接 题意:给定一个01数量相等的二进制字符串,每次操作可以旋转一段子串,问至少多少次操作可以使得所有相邻字符都不一样. 思路:对于某个"00"开头(结尾),"11& ...
- 贪心 ---- Codeforces Global Round 8,B. Codeforces Subsequences[贪心,贪的乘法原理]
题目链接 给出字符串,统计子串(子串字母可以跳跃)是codeforces的数量. 本题要求,给出子串最少数量k,构造字符串s,要求字符串s包含的字母数量最少,输出这个最少的字符串s. 题目要求是至少有 ...
- Codeforces 985C (贪心)
传送门 题面: C. Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input ...
- CodeForces - 93B(贪心+vectorpairint,double +double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)
http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 1 #include <cstd ...
- 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)
链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...
- A/B Matrix CodeForces - 1360G(思维构造)
You are given four positive integers n, m, a, b (1≤b≤n≤50; 1≤a≤m≤50). Find any such rectangular matr ...
最新文章
- 2022-2028年中国玻璃纤维毡热塑性塑料(GMT)行业市场研究及前瞻分析报告
- replace函数对dataframe中的值进行替换(所有数据列中的相同值)
- Android自带语音播报+讯飞语音播报封装(直接用)
- 线程池中阻塞队列的作用?为什么是先添加列队而不是先创建最大线程?线程池中线程复用原理
- AKKA框架持久化入门样例
- 17、mybatis两个内置参数
- 1个不为人知的 Jupyter notebook 使用技巧,今天分享出来。
- informix数据库 java 增删改查
- 20145325张梓靖 《信息安全系统设计基础》第10周学习总结
- python调用excel的宏_配置Office Excel运行Python宏脚本
- 「电商干货」分销爆单的6个步骤
- 在线python编程网页-手把手教你用Python写一个在线网站,这真的很装逼!
- 试用期合同可以随时离职吗
- 计算机窗口弹出多个窗口,电脑怎么打开多个微信窗口
- 【数据结构】CH3 栈和队列
- ARM嵌入式开发板推荐
- 注意!多款智能家居Hub存在远程代码执行漏洞
- load forecasting(一)
- #每天一道算法题:出现一次与出现k次的数
- 中国互联网乃至移动互联网发展最好的十个城市排名,我会这么排:北京,深圳,上海,杭州,广州,成都,武汉,南京,西安,厦门。
热门文章
- linux 串口 dma,STM32 USART串口DMA 接收和发送流程详解
- SpringBoot→初始化项目just run@SpringBootApplication、请求处理@RequestMapping、属性配置yml
- 计算机科学基础word实验一,大学计算机基础综合实验实验报告 参考模板(1)
- java中实现方法重载怎么声明_java 接口中如何声明类似于重载的方法?
- SQL Server高级查询之子查询(子查询非典型应用)
- window.onload和jQuery的ready函数区别
- 无重复字符最长字串的滑动窗口结合哈希表解法(注释详尽)
- [Python + PyQt5] 均匀平面波的入射、反射及透射的仿真实验
- 非华为电脑配对华为手机(RMB+5899¥)
- java扫描指定主机的端口socket服务