水题,很多人WA了,不知道怎么回事,可能...题意比较绕?
#include <cstdio>
#include <iostream>
#include <vector>
#include <string.h>
#include <map>
#include <algorithm>
#include <set>
#include <math.h>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=200005;
int main() {
int n,a,b,i,x;
scanf("%d%d%d",&n,&a,&b);
int ans=0;
int nowa=a;
int nowb=2*b;
int nowc=b;
for (i=1;i<=n;i++) {
scanf("%d",&x);
if (x==1) {
if (nowa>0) nowa--; else
if (nowb>0&&nowc>0) nowc--,nowb--;
else if (nowb>0) nowb--;
else ans++;
} else {
if (nowc>0) nowc--,nowb-=2; else ans+=2;
}
}
printf("%d",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 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 <cstdio>
#include <iostream>
#include <vector>
#include <string.h>
#include <map>
#include <algorithm>
#include <set>
#include <math.h>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
char ma[105][105];
int main() {
int n,m,i,j;
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++) {
scanf("%s",ma[i]+1);
}
int l=105,r=-1,u=105,d=-1,sum=0;
for (i=1;i<=n;i++) {
for (j=1;j<=m;j++) {
if (ma[i][j]=='B') {
l=min(j,l);
r=max(j,r);
u=min(i,u);
d=max(i,d);
sum++;
}
}
}
if (sum==0) cout << 1 << endl; else {
int len=max(d-u+1,r-l+1);
for (i=1;i<=n;i++) {
for (j=1;j<=m;j++) {
if (j+len-1>=r&&j<=l&&i+len-1>=d&&i<=u&&i+len-1<=n&&j+len-1<=m) {
cout << len*len-sum << endl;
return 0;
}
}
}
cout << -1 << endl;
}
return 0;
}
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
The first line contains single integer n (1 ≤ n ≤ 105) — the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then kidistinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order — positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 ≤ xi, j ≤ 106, 1 ≤ ki ≤ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Print lexicographically minimal string that fits all the information Ivan remembers.
3a 4 1 3 5 7ab 2 1 5ca 1 4
abacaba
1a 1 3
aaa
3ab 1 1aba 1 3ab 2 3 5
ababab
#include <cstdio>
#include <iostream>
#include <vector>
#include <string.h>
#include <map>
#include <algorithm>
#include <set>
#include <math.h>
#include <cmath>
#include <queue>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=2000005;
int s[maxn],f[maxn];
char a[maxn];
int find(int now) {
if (f[now]==now) return now; else {
f[now]=find(f[now]);
return f[now];
}
}
int main() {
mem0(s);
int n,i,j,m,len,x,maxlen=0;
scanf("%d",&n);
for (i=1;i<maxn;i++) f[i]=i;
for (i=1;i<=n;i++) {
scanf("%s",a);
len=strlen(a);
scanf("%d",&m);
for (j=1;j<=m;j++) {
scanf("%d",&x);
maxlen=max(maxlen,x+len-1);
int pos=find(x);
while (pos<=x+len-1) {
s[pos]=(int)a[pos-x];
f[pos]=pos+1;
pos=find(pos);
}
}
}
for (i=1;i<=maxlen;i++) {
if (s[i]) printf("%c",(char)s[i]); else printf("a");
}
return 0;
}
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability.
Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes.
Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.
The first line contains two integers n and k (3 ≤ n ≤ 2·105, 2 ≤ k ≤ n - 1) — the total number of nodes and the number of exit-nodes.
Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.
In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next n - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to n. Exit-nodes can have any ids.
If there are multiple answers, print any of them.
3 2
2 1 2 2 3
5 3
3 1 2 2 3 3 4 3 5
In the first example the only network is shown on the left picture.
In the second example one of optimal networks is shown on the right picture.
Exit-nodes are highlighted.
![](https://i-blog.csdnimg.cn/blog_migrate/59f9364f6219302cac3b614b37229914.png)
给定一棵树的总节点数和叶节点数,构造一棵树,使所有叶节点中的任意点对的最大间距最小。
要使距离最小,树的层数要尽可能小,因为叶节点之间的距离就是两个节点的层数之和。而叶节点必须为k个,所以我们每一层最多放k个节点,再多的话,叶节点就多于k个了。
放置方法:先放置根节点,再逐层放k个节点。树的第二层k个节点全部连根节点,第三层开始k个节点分别和上一层的任意一个节点相连。
画个图,最短距离可以直接由此推出来。
如:n=8,k=3时,构造出的树如下图
代码:
#include <cstdio>
#include <iostream>
#include <vector>
#include <string.h>
#include <map>
#include <algorithm>
#include <set>
#include <math.h>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
int main() {
int n,k;
scanf("%d%d",&n,&k);
int ans,i,j;
if (k!=2) {
if (n%k==2) ans=(n-2)/k*2+1; else ans=(n-2)/k*2+2;
} else ans=n-1;
printf("%d\n",ans);
j=1;i=1;
int cnt=0;
while (cnt<n-1) {
cnt++;
i++;
if (j==1) printf("%d %d\n",j,i); else printf("%d %d\n",i-k,i);
if (cnt%k==0) j=i;
}
return 0;
}