第一次参加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 4 WWWW WWWB WWWB WWBB WWWW
5
1 2 BB
-1
3 3 WWW WWW WWW
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 0x3f3f3f3f
int 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);}}}
//待补充