Codeforces Round #423 Div.2 A B C D E F

来源:互联网 发布:淘宝stefano ricci皮带 编辑:程序博客网 时间:2024/06/06 00:26

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 2clients.



水题,很多人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;}

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.


一个方阵,每个格子的颜色是黑白中的一种。找一个正方形,覆盖所有黑格子。问正方形里最少几个白格子。如果没有黑格子输出1,无解输出-1.

找到所有黑格子上下左右四个方向的极限位置,由此求得正方形的最小边长,再穷举正方形左上角看是否有满足条件的正方形存在。

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

C. String Reconstruction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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 1061 ≤ xi, j ≤ 1061 ≤ 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.

Output

Print lexicographically minimal string that fits all the information Ivan remembers.

Examples
input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
output
abacaba
input
1
a 1 3
output
aaa
input
3
ab 1 1
aba 1 3
ab 2 3 5
output
ababab

一个字符串不知道,给你多个这个字符串的子串和起始位置,让你推出字典序最小的满足条件的字符串。

每次更新最大长度和可以确定的字符,然而字符串很长,重复更新很多,直接暴力必定超时。
为了避免超时,可以使用并查集。并查集中查找到的数值find(i)表示从位置i开始,第一个没有确定的字符的位置。
开始时,f[i]=i.每更新一个字符,就将i位置与下一个位置i+1相连。这样最终查找到的总是第一个没有确定的位置。

AC代码,324ms:
#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;}



D. High Load
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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.

Input

The first line contains two integers n and k (3 ≤ n ≤ 2·1052 ≤ 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.

Output

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.

Examples
input
3 2
output
21 22 3
input
5 3
output
31 22 33 43 5
Note

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.


给定一棵树的总节点数和叶节点数,构造一棵树,使所有叶节点中的任意点对的最大间距最小。


要使距离最小,树的层数要尽可能小,因为叶节点之间的距离就是两个节点的层数之和。而叶节点必须为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;}

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", "G", "C". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string s initially.

Evolution of the species is described as a sequence of changes in the DNA. Every change is a change of some nucleotide, for example, the following change can happen in DNA strand "AAGC": the second nucleotide can change to "T" so that the resulting DNA strand is "ATGC".

Scientists know that some segments of the DNA strand can be affected by some unknown infections. They can represent an infection as a sequence of nucleotides. Scientists are interested if there are any changes caused by some infections. Thus they sometimes want to know the value of impact of some infection to some segment of the DNA. This value is computed as follows:

  • Let the infection be represented as a string e, and let scientists be interested in DNA strand segment starting from position l to position r, inclusive.
  • Prefix of the string eee... (i.e. the string that consists of infinitely many repeats of string e) is written under the string s from position l to position r, inclusive.
  • The value of impact is the number of positions where letter of string s coincided with the letter written under it.

Being a developer, Innokenty is interested in bioinformatics also, so the scientists asked him for help. Innokenty is busy preparing VK Cup, so he decided to delegate the problem to the competitors. Help the scientists!

Input

The first line contains the string s (1 ≤ |s| ≤ 105) that describes the initial DNA strand. It consists only of capital English letters "A", "T", "G" and "C".

The next line contains single integer q (1 ≤ q ≤ 105) — the number of events.

After that, q lines follow, each describes one event. Each of the lines has one of two formats:

  • 1 x c, where x is an integer (1 ≤ x ≤ |s|), and c is a letter "A", "T", "G" or "C", which means that there is a change in the DNA: the nucleotide at position x is now c.
  • 2 l r e, where lr are integers (1 ≤ l ≤ r ≤ |s|), and e is a string of letters "A", "T", "G" and "C" (1 ≤ |e| ≤ 10), which means that scientists are interested in the value of impact of infection e to the segment of DNA strand from position l to position r, inclusive.
Output

For each scientists' query (second type query) print a single integer in a new line — the value of impact of the infection on the DNA.

Examples
input
ATGCATGC42 1 8 ATGC2 2 6 TTT1 4 T2 2 6 TA
output
824
input
GAGTTGTTAA62 3 4 TATGGTG1 1 T1 6 G2 5 9 AGTAATA1 10 G2 2 6 TTGT
output
031
Note

Consider the first example. In the first query of second type all characters coincide, so the answer is 8. In the second query we compare string "TTTTT..." and the substring "TGCAT". There are two matches. In the third query, after the DNA change, we compare string "TATAT..."' with substring "TGTAT". There are 4 matches.



这题乍一看是个字符串题,其实是数据结构题。

题意:给你一个字符串s, q次询问,每次询问给你一个串e,问将e无限循环放置,即构成字符串eeeee...之后,和

s串从l到r的区间相比有多少个字符相同。每一个串都仅由ATCG四种字符构成。

相关题目:http://acm.uestc.edu.cn/#/problem/show/1581

BZOJ 3922

由于只给了四种字符,查询串长度最大仅为10,所以可以开200个线段树,存每一段下标mod1,2,3...10为对应每

个可能答案的字符的个数。

当然,这题用树状数组实现更简单。

放一个bzoj3922/uestc1581的代码吧:

#include <cstdio>#include <iostream>#include <string.h>using namespace std;typedef long long ll;const int maxn=70005;const int maxk=5;const int treenum=maxk*(maxk+1)/2;struct Tree {int l,r,lc,rc;ll max[treenum];};Tree tree[maxn*3];int num;ll a[maxn];int b[maxk+1][maxk+1];ll max(ll a,ll b) {if (a>b) return a; else return b;}void build(int now,int l,int r) {tree[now].l=l;tree[now].r=r;if (l==r) {memset(tree[now].max,-0x2,sizeof(tree[now].max));scanf("%lld",&a[l]);for (int i=1;i<=maxk;i++)     tree[now].max[b[i][l%i]]=a[l];} else {num++;tree[now].lc=num;build(num,l,(l+r)/2);num++;tree[now].rc=num;build(num,(l+r)/2+1,r);for (int i=0;i<treenum;i++) tree[now].max[i]=max(tree[tree[now].lc].max[i],tree[tree[now].rc].max[i]);}//cout << now << ' ' << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].max[3] << endl;}void update(int now,int pos,int c) {if (pos==tree[now].l&&pos==tree[now].r) {for (int i=1;i<=maxk;i++)     tree[now].max[b[i][pos%i]]+=c;} else {if (pos<=(tree[now].l+tree[now].r)/2)     update(tree[now].lc,pos,c);if (pos>(tree[now].l+tree[now].r)/2)     update(tree[now].rc,pos,c);for (int i=1;i<=maxk;i++)     tree[now].max[b[i][pos%i]]=max(tree[tree[now].lc].max[b[i][pos%i]],tree[tree[now].rc].max[b[i][pos%i]]);    }}ll findmax(int now,int l,int r,int sn) {//cout << now << ' ' << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].max[sn] << endl;if (l<=tree[now].l&&r>=tree[now].r) {return tree[now].max[sn];} else if (r<=tree[tree[now].lc].r) return findmax(tree[now].lc,l,r,sn); else   if (l>=tree[tree[now].rc].l) return findmax(tree[now].rc,l,r,sn); else          return max(findmax(tree[now].lc,l,r,sn),findmax(tree[now].rc,l,r,sn));}int main() {int i,j,n,m,c,x,y;scanf("%d",&n);num=0;for (i=1;i<=maxk;i++) for (j=0;j<i;j++)    b[i][j]=num++;num=1;build(1,1,n);scanf("%d",&m);for (i=1;i<=m;i++) {scanf("%d%d%d",&x,&y,&c);if (x==1) {if (c<=maxk)    printf("%lld\n",findmax(1,y,n,b[c][y%c]));    else {    ll ans=-0x3f3f3f3f3f;    for (j=y;j<=n;j+=c)         ans=max(ans,a[j]);    printf("%lld\n",ans);    }}else {a[y]+=c;    update(1,y,c);}}return 0;}/*101 6 1 4 9 4 8 2 8 5101 3 30 5 40 3 81 2 51 4 81 7 51 3 60 1 21 5 31 4 9*/

D. Best Edge Weight

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a connected weighted graph with n vertices and m edges. The graph doesn't contain loops nor multiple edges. Consider some edge with id i. Let's determine for this edge the maximum integer weight we can give to it so that it is contained in all minimum spanning trees of the graph if we don't change the other weights.

You are to determine this maximum weight described above for each edge. You should calculate the answer for each edge independently, it means there can't be two edges with changed weights at the same time.

Input

The first line contains two integers n and m (2 ≤ n ≤ 2·105n - 1 ≤ m ≤ 2·105), where n and m are the number of vertices and the number of edges in the graph, respectively.

Each of the next m lines contains three integers uv and c (1 ≤ v, u ≤ nv ≠ u1 ≤ c ≤ 109) meaning that there is an edge between vertices u and v with weight c.

Output

Print the answer for each edge in the order the edges are given in the input. If an edge is contained in every minimum spanning tree with any weight, print -1 as the answer.

Examples
input
4 41 2 22 3 23 4 24 1 3
output
2 2 2 1 
input
4 31 2 22 3 23 4 2
output
-1 -1 -1 




F题题解+AC代码:戳这里

利用树上倍增解决
原创粉丝点击