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

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·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 2
1 2 1 1
output
0
input
4 1 1
1 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
", 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 4
WWWW
WWWB
WWWB
WWBB
WWWW
output
5
input
1 2
BB
output
-1
input
3 3
WWW
WWW
WWW
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
", (maxlen + 1) * (maxlen + 1) - cnt);
90     }
91 }
92 
93 int main() {
94     init();
95     solve();
96     return 0;
97 }
原文地址:https://www.cnblogs.com/yyf0309/p/7156409.html