这场据说很难,但是做起来感觉还好的
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.
The first line contains three integers n, a 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.
Print the total number of people the restaurant denies service to.
4 1 21 2 1 1
0
4 1 11 1 2 1
2
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.
水题啊,直接上题意
#include <bits/stdc++.h> using namespace std; int main() { int ans=0,c=0,n,a,b; cin>>n>>a>>b; for(int i=0; i<n; i++) { int x; scanf("%d",&x); if(x==1) { if(a) a--; else if(b) c++,b--; else if(c) c--; else if(!a&&!b) ans++; } else { if(b) b--; else ans+=2; } } printf("%d\n",ans); return 0; }
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<bits/stdc++.h> using namespace std; int main() { int n,m,ans=0; cin>>n>>m; char a; int imi=105,jmi=105,ima=0,jma=0; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { cin>>a; if(a=='B') { ans++; imi=min(imi,i); ima=max(ima,i); jmi=min(jmi,j); jma=max(jma,j); } } int k=max(ima-imi,jma-jmi)+1; if(!ans) printf("1\n"); else if(n>=k&&m>=k) printf("%d\n",k*k-ans); else printf("-1\n"); return 0; }
来源:https://www.cnblogs.com/BobHuang/p/7181323.html