Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

Pronlem A

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·105, 1 ≤ 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.



  注意读题,客人先愿意坐空桌子(当然单个客人会先选择单人桌)!题目跳着跳着看,然后吃大亏了。。。

Code

 1 /**
 2  * Codeforces
 3  * Problem#828A
 4  * Accepted
 5  * Time:15ms
 6  * Memory:2056k
 7  */
 8 #include <iostream>
 9 #include <cstdio>
10 #include <ctime>
11 #include <cmath>
12 #include <cctype>
13 #include <cstring>
14 #include <cstdlib>
15 #include <fstream>
16 #include <sstream>
17 #include <algorithm>
18 #include <map>
19 #include <set>
20 #include <stack>
21 #include <queue>
22 #include <vector>
23 #include <stack>
24 #ifndef WIN32
25 #define Auto "%lld"
26 #else
27 #define Auto "%I64d"
28 #endif
29 using namespace std;
30 typedef bool boolean;
31 const signed int inf = (signed)((1u << 31) - 1);
32 const signed long long llf = (signed long long)((1ull << 61) - 1);
33 const double eps = 1e-6;
34 const int binary_limit = 128;
35 #define smin(a, b) a = min(a, b)
36 #define smax(a, b) a = max(a, b)
37 #define max3(a, b, c) max(a, max(b, c))
38 #define min3(a, b, c) min(a, min(b, c))
39 template<typename T>
40 inline boolean readInteger(T& u){
41     char x;
42     int aFlag = 1;
43     while(!isdigit((x = getchar())) && x != ‘-‘ && x != -1);
44     if(x == -1) {
45         ungetc(x, stdin);
46         return false;
47     }
48     if(x == ‘-‘){
49         x = getchar();
50         aFlag = -1;
51     }
52     for(u = x - ‘0‘; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - ‘0‘);
53     ungetc(x, stdin);
54     u *= aFlag;
55     return true;
56 }
57
58 int n, a, b, c = 0;
59 int cnt = 0;
60
61 inline void init() {
62     readInteger(n);
63     readInteger(a);
64     readInteger(b);
65 }
66
67 inline void solve() {
68     for(int i = 1, x; i <= n; i++) {
69         readInteger(x);
70         if(x == 1) {
71             if(a)
72                 a--;
73             else if(b)
74                 c += 1, b -= 1;
75             else if(c)
76                 c--;
77             else
78                 cnt++;
79         } else {
80             if(!b)
81                 cnt += 2;
82             else
83                 b--;
84         }
85 //        cout << a << " " << b << endl;
86     }
87     printf("%d\n", cnt);
88 }
89
90 int main() {
91     init();
92     solve();
93     return 0;
94 }

Problem A



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.



  找出最大的纵坐标之差和横坐标之差,然后判一下边界就很轻松了。

Code

 1 /**
 2  * Codeforces
 3  * Problem#828B
 4  * Accepted
 5  * Time:15ms
 6  * Memory:2100k
 7  */
 8 #include <iostream>
 9 #include <cstdio>
10 #include <ctime>
11 #include <cmath>
12 #include <cctype>
13 #include <cstring>
14 #include <cstdlib>
15 #include <fstream>
16 #include <sstream>
17 #include <algorithm>
18 #include <map>
19 #include <set>
20 #include <stack>
21 #include <queue>
22 #include <vector>
23 #include <stack>
24 #ifndef WIN32
25 #define Auto "%lld"
26 #else
27 #define Auto "%I64d"
28 #endif
29 using namespace std;
30 typedef bool boolean;
31 const signed int inf = (signed)((1u << 31) - 1);
32 const signed long long llf = (signed long long)((1ull << 61) - 1);
33 const double eps = 1e-6;
34 const int binary_limit = 128;
35 #define smin(a, b) a = min(a, b)
36 #define smax(a, b) a = max(a, b)
37 #define max3(a, b, c) max(a, max(b, c))
38 #define min3(a, b, c) min(a, min(b, c))
39 template<typename T>
40 inline boolean readInteger(T& u){
41     char x;
42     int aFlag = 1;
43     while(!isdigit((x = getchar())) && x != ‘-‘ && x != -1);
44     if(x == -1) {
45         ungetc(x, stdin);
46         return false;
47     }
48     if(x == ‘-‘){
49         x = getchar();
50         aFlag = -1;
51     }
52     for(u = x - ‘0‘; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - ‘0‘);
53     ungetc(x, stdin);
54     u *= aFlag;
55     return true;
56 }
57
58 int n, m;
59 char mmap[105][105];
60 int maxx = 0, minx = inf, maxy = 0, miny = inf;
61 int cnt = 0;
62
63 inline void init() {
64     readInteger(n);
65     readInteger(m);
66     gets(mmap[0]);
67     for(int i = 1; i <= n; i++) {
68         gets(mmap[i] + 1);
69         for(int j = 1; j <= m; j++) {
70             if(mmap[i][j] == ‘W‘)    continue;
71             smax(maxx, i);
72             smax(maxy, j);
73             smin(minx, i);
74             smin(miny, j);
75             cnt++;
76         }
77     }
78 }
79
80 inline void solve() {
81     if(minx == inf) {
82         puts("1");
83         return;
84     }
85     int maxlen = max(maxx - minx, maxy - miny);
86     if(maxlen >= m || maxlen >= n) {
87         puts("-1");
88     } else {
89         printf("%d\n", (maxlen + 1) * (maxlen + 1) - cnt);
90     }
91 }
92
93 int main() {
94     init();
95     solve();
96     return 0;
97 }
时间: 2024-10-11 10:33:11

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B的相关文章

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)D. High Load

题意:出n个点,其中k个叶子节点,问构造出的树最远的两个点最近是多少 思路:以一个点为中心,然后m个伸出,一层层扩散,(n-1)%m==k,如果k==0,即可以平分,长度就是2*(n-1)/m,如果取模为k==1,说明多出一个,+1,其他的话,就是最后一层补k个,但是最长的还是+2 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int main(){ 5 int n,m; 6 cin>>n>>m; 7 int

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) B. Black Square

题意:问是否可以形成一个全黑正方形 思路:可以找出正方形的边,然后判断下这个矩阵是否容得下,n,m都比边短,比赛的时候写麻烦了,还去找了这个正方形究竟在哪个位置,这样的话得考虑很多情况,不如就边*边-黑子的总数 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=2e5+10; 5 char a[102][102]; 6 int main(){ 7 int n,m; 8 s

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction

题意:给出各个字符串出现的起始位置,问整个的字符串是什么,(字典序最小) 思路:开始写的是用set+优先队列存取每个位置出现的最长字符串,然后遍历,爆内存...爆...内...存...我们可以用并查集,已经确认的位置他们并在一起,指向后面第一个没有被确认的(看代码理解吧) 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=2e6+10; 4 5 int n,fa[N]; 6 char s[N],b[N]; 7 8 in

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E DNA Evolution

DNA Evolution 题目让我们联想到树状数组或者线段树,但是如果像普通那样子统计一段的和,空间会爆炸. 所以我们想怎样可以表示一段区间的字符串. 学习一发大佬的解法. 开一个C[10][10][4][n],就可以啦,第二维表示e的长度,第一维表示i%e的长度,第三维表示颜色,第四维求和了. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5+5; 4 char s[maxn]; 5 map&l

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组

E. DNA Evolution time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)爆零记

昨晚一个瓜皮说今晚有cf,听说是晚间场,我瞅了一眼,娃,VK Cup,上分的好机会,看着比赛时间就有点心酸了,0:35,当时一直在纠结要不要打的问题,当时想着应该不难吧,要不打一下吧,要不还是看看题先,如果容易就打,难的话就不打了好的吧!于是就这样愉快的决定了.......cf日常延时10分钟,0:45,要不要去睡觉啊,干脆先睡一觉好了,然后又是忍不住诱惑在等待开始! 比赛一开始,瞅了一眼A,这不是一道水题嘛,直接敲啊,然后1分钟就搞定了,交了就过了,B题直接求边界点就好了,扫了一遍就过了,C题

Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals)

A.String Reconstruction B. High Load C. DNA Evolution 题意:给定一个只包含A,T,C,G的字符串S,有如下两种操作 1)修改一个点的字母. 2)给定一个字符串e ($\left | e \right |\leq 10$),生成一个由e重复组成的新串,eee...,问$S_{l..r}$中有几个字母跟这个新的字符串一一对应. SOL:对于每个字母,用BIT[x][y][L]表示$S_{1..L}$中,所有$\equiv x\left (mod

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力

Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is a

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebo