#423

来源:互联网 发布:托福 一个月 知乎 编辑:程序博客网 时间:2024/06/07 03:47

第一次参加CF比赛T^T真是惊险刺激啊!大佬太多了!!!膜拜大佬!

A. Restaurant Tables
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In a small restaurant there are a tables for one person and b tables for two persons.

It it known that n groups of people come today, each consisting of one or two people.

If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.

If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.

You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.

Input

The first line contains three integers na and b (1 ≤ n ≤ 2·1051 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.

The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.

Output

Print the total number of people the restaurant denies service to.

Examples
input
4 1 21 2 1 1
output
0
input
4 1 11 1 2 1
output
2
Note

In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.

In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.



//一开始没用c记录两座已有一个人坐了的,然后就……

#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);} 




B. Black Square
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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.

Examples
input
5 4WWWWWWWBWWWBWWBBWWWW
output
5
input
1 2BB
output
-1
input
3 3WWWWWWWWW
output
1
Note

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);}}} 

//待补充