#423
来源:互联网 发布:托福 一个月 知乎 编辑:程序博客网 时间:2024/06/07 03:47
第一次参加CF比赛T^T真是惊险刺激啊!大佬太多了!!!膜拜大佬!
#include<iostream>#include<cstdio>using namespace std;int main(){int n, a, b, c,num, ans = 0;scanf("%d %d %d",&n, &a, &b);c = b;while(n--){scanf("%d",&num);if(num == 2){if(b > 0 && c > 0){b--;c--;}elseans += 2;}else if(num == 1){if(a > 0)a--;else if(b > 0){b --;}else if(c > 0){c--;}else ans++;}}printf("%d\n",ans);}
Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.
You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.
The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the sizes of the sheet.
The next n lines contain m letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.
Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.
5 4WWWWWWWBWWWBWWBBWWWW
5
1 2BB
-1
3 3WWWWWWWWW
1
In the first example it is needed to paint 5 cells — (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2).
In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.
In the third example all cells are colored white, so it's sufficient to color any cell black.
//分析:特殊情况要分清楚,黑色细胞数确定, 只需确定构成正方形的最小边长
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define INF 0x3f3f3f3fint main(){int n, m, i, j, right = 0, left = INF, up = INF, down = 0, sum = 0, ans = 0, flag = 1;char ch, space;scanf("%d %d", &n, &m);space = getchar();for(i = 0; i < n; ++i){for(j = 0; j < m ; ++j){ch = getchar();if(ch == 'B'){sum++;right = max(right, j);left = min(left, j);up = min(up, i);down = max(down, i);}else{flag = 0;}}space = getchar();}if(flag){if(n == m)printf("0\n");elseprintf("-1\n");}else if(sum <= 0){printf("1\n");}else if(!flag && sum){ans = max((right - left + 1), (down - up + 1));if(ans > n || ans > m)printf("-1\n");else{printf("%d\n",(ans*ans) - sum);}}}
//待补充
- 423
- 423
- 423
- #423
- lintcode-423
- ocp-423
- 423C
- 423D
- UVA 423
- spoj 423 Assignments
- Uva 423 - MPI Maelstrom
- UVa 423 - MPI Maelstrom
- 053第423题
- (423)Valid Parentheses
- #423 Valid Parentheses
- LintCode 423:Valid Parentheses
- shu 423 密码破解
- 423ARestaurant Tables
- SqlServerCLR聚合函数:分组连接字符串
- 轻松理解PV操作(转)
- 个简单的学生登陆系统的jsp实现之登陆身份验证
- unbuntu系统 在虚拟机下的安装
- 内部类向上转型为接口
- #423
- SAP物料主数据采购视图采购价值代码设置
- 简单web导航栏
- Hadoop 新 MapReduce 框架 Yarn 详解
- CSDN-markdown编辑器参考
- 敏捷之MDE认知
- 监听浏览器后退事件,使其转向指定URL,控制某些页面不能返回
- 深度学习环境搭建:linux下 Ubuntu16.04+cuda8.0+cudnn+anaconda+tensorflow并配置远程访问jupyter notebook
- 算法概论课本第八章习题作业 8.8