Codeforces Round #294 (Div. 2)A.B.C.D

来源:互联网 发布:s3c2440a数据手册 编辑:程序博客网 时间:2024/05/16 16:06

A. A and B and Chess
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A and B are preparing themselves for programming contests.

To train their logical thinking and solve problems better, A and B decided to play chess. During the game A wondered whose position is now stronger.

For each chess piece we know its weight:

  • the queen's weight is 9,
  • the rook's weight is 5,
  • the bishop's weight is 3,
  • the knight's weight is 3,
  • the pawn's weight is 1,
  • the king's weight isn't considered in evaluating position.

The player's weight equals to the sum of weights of all his pieces on the board.

As A doesn't like counting, he asked you to help him determine which player has the larger position weight.

Input

The input contains eight lines, eight characters each — the board's description.

The white pieces on the board are marked with uppercase letters, the black pieces are marked with lowercase letters.

The white pieces are denoted as follows: the queen is represented is 'Q', the rook — as 'R', the bishop — as'B', the knight — as 'N', the pawn — as 'P', the king — as 'K'.

The black pieces are denoted as 'q', 'r', 'b', 'n', 'p', 'k', respectively.

An empty square of the board is marked as '.' (a dot).

It is not guaranteed that the given chess position can be achieved in a real game. Specifically, there can be an arbitrary (possibly zero) number pieces of each type, the king may be under attack and so on.

Output

Print "White" (without quotes) if the weight of the position of the white pieces is more than the weight of the position of the black pieces, print "Black" if the weight of the black pieces is more than the weight of the white pieces and print "Draw" if the weights of the white and black pieces are equal.

Examples
Input
...QK......................................................rk...
Output
White
Input
rnbqkbnrpppppppp................................PPPPPPPPRNBQKBNR
Output
Draw
Input
rppppppr...k....................................K...Q...........
Output
Black
Note

In the first test sample the weight of the position of the white pieces equals to 9, the weight of the position of the black pieces equals 5.

In the second test sample the weights of the positions of the black and the white pieces are equal to 39.

In the third test sample the weight of the position of the white pieces equals to 9, the weight of the position of the black pieces equals to 16.


题目大意:

白色的是大写棋子,黑色的是小写棋子,对应每种棋子都有权值,获胜的条件就是自己的棋子权值和大于对方,问谁能赢。


思路:

模拟即可。

#include<stdio.h>#include<string.h>using namespace std;char a[10][10];int main(){    int w,b;    w=0,b=0;    for(int i=0;i<8;i++)    {        scanf("%s",a[i]);        for(int j=0;j<8;j++)        {            if(a[i][j]>='A'&&a[i][j]<='Z')            {                a[i][j]+=32;                if(a[i][j]=='q')w+=9;                if(a[i][j]=='r')w+=5;                if(a[i][j]=='b')w+=3;                if(a[i][j]=='n')w+=3;                if(a[i][j]=='p')w+=1;            }            else            {                if(a[i][j]=='q')b+=9;                if(a[i][j]=='r')b+=5;                if(a[i][j]=='b')b+=3;                if(a[i][j]=='n')b+=3;                if(a[i][j]=='p')b+=1;            }        }    }   // printf("%d %d\n",w,b);    if(w>b)printf("White\n");    if(w<b)printf("Black\n");    if(w==b)printf("Draw\n");}

B. A and B and Compilation Errors
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A and B are preparing themselves for programming contests.

B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.

Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake.

However, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared — the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change.

Can you help B find out exactly what two errors he corrected?

Input

The first line of the input contains integer n (3 ≤ n ≤ 105) — the initial number of compilation errors.

The second line contains n space-separated integersa1, a2, ..., an (1 ≤ ai ≤ 109) — the errors the compiler displayed for the first time.

The third line contains n - 1 space-separated integersb1, b2, ..., bn - 1 — the errors displayed at the second compilation. It is guaranteed that the sequence in the third line contains all numbers of the second string except for exactly one.

The fourth line contains n - 2 space-separated integersс1, с2, ..., сn - 2 — the errors displayed at the third compilation. It is guaranteed that the sequence in the fourth line contains all numbers of the third line except for exactly one.

Output

Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively.

Examples
Input
51 5 8 123 7123 7 5 15 1 7
Output
8123
Input
61 4 3 3 5 73 7 5 4 34 3 7 5
Output
13
Note

In the first test sample B first corrects the error number 8, then the error number 123.

In the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step. 


题目大意:

这个人一共处理了两个bug,一开始有N个bug,调试一次之后有N-1个bug,再调试一次之后还有N-2个bug,问哪两个bug被调试掉了,按照顺序输出。


思路:


首先将初始和第一次调试的结果放在一个数组中,那么问题就变成了:哪个数字出现的次数为1,保证其他所有数字出现次数为2.

根据异或的特性:A^A=0,那么我们将这个数组值整个异或起来得到的解就是第一次调试出来的bug.

那么同理,第二次调试的结果也如此得出即可。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;int a[1000500];int b[1000500];int c[1000500];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)scanf("%d",&a[i]);        for(int j=0;j<n-1;j++)scanf("%d",&b[j]);        for(int k=0;k<n-2;k++)scanf("%d",&c[k]);        int ans1=0,ans2=0;;        for(int i=0;i<n;i++)ans1^=a[i];        for(int j=0;j<n-1;j++)ans1^=b[j],ans2^=b[j];        for(int k=0;k<n-2;k++)ans2^=c[k];        printf("%d\n%d\n",ans1,ans2);    }}

C. A and B and Team Training
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A and B are preparing themselves for programming contests.

An important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solving problems together with experienced participants.

A believes that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people.

However, B believes that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience.

As a result, A and B have decided that all the teams during the training session should belong to one of the two types described above. Furthermore, they agree that the total number of teams should be as much as possible.

There are n experienced members and m newbies on the training session. Can you calculate what maximum number of teams can be formed?

Input

The first line contains two integers n andm (0 ≤ n, m ≤ 5·105) — the number of experienced participants and newbies that are present at the training session.

Output

Print the maximum number of teams that can be formed.

Examples
Input
2 6
Output
2
Input
4 5
Output
3
Note

Let's represent the experienced players as XP and newbies as NB.

In the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB).

In the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB).


题目大意:

一共有N个经验丰富的队员和M个经验不丰富的队员,组队需要三个人,组队有两种方式:

①一个经验丰富的带两个不丰富的。

②两个经验丰富的带一个不丰富的。

问最多能够组多少个队伍。


思路:


如果经验丰富的人数多于经验不丰富的人数,那么我们肯定组出一个方式②的队伍。

如果经验丰富的人数小于经验不丰富的人数,那么我们肯定组出一个方式①的队伍。

那么接下来模拟即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        int ans=0;        while(1)        {            if(n==0||m==0)break;            if(n>m)swap(n,m);            if(n<1||m<2)break;            n-=1;            m-=2;            ans++;        }        printf("%d\n",ans);    }}

D. A and B and Interesting Substrings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A and B are preparing themselves for programming contests.

After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes.

A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes).

B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one).

Also, A and B have a string s. Now they are trying to find out how many substringst of a string s are interesting to B (that is,t starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A),except for the first and the last one is equal to zero.

Naturally, A and B have quickly found the number of substrings t that are interesting to them. Can you do it?

Input

The first line contains 26 integers xa, xb, ..., xz ( - 105 ≤ xi ≤ 105) — the value assigned to letters a, b, c, ..., z respectively.

The second line contains string s of length between1 and 105 characters, consisting of Lating lowercase letters— the string for which you need to calculate the answer.

Output

Print the answer to the problem.

Examples
Input
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1xabcab
Output
2
Input
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1aaa
Output
2
Note

In the first sample test strings satisfying the condition above are abca and bcab.

In the second sample test strings satisfying the condition above are two occurences ofaa.


题目大意:

给你26个字母的权值,问你能够找到多少个子段,使得去掉头和尾的权值和为0,  需要保证头和尾的字符是一样的。


思路:


1、我们分析样例1的前缀和:

1 2 3 2 3 4

x a b c a b

我们不难发现,对应两个相同字符位于位子i,j(i<j)&&sum【i】+val【位子i的字符】==sum【j】,那么中间的部分就一定是和为0的部分。那么这一部分的子段就可以作为一个合法串进行计数。


2、那么我们扫26次,每次扫一个字符,每次都对应扫这个前缀和,对应当前扫到的位子j,如果位子j上的字符是本次需要判断的字符,那么对应看前边有多少个sum【i】==sum【j】-val【这个字符】的位子i.有一个,计数器就要进行+1操作.


3、但是暴力搞需要O(26n^2)的操作,所以我们这里使用map进行计数,map【i】表示和为i的情况有多少个,那么对应扫到位子j的时候:

output+=map【sum【j】-val【这个字符】】即可。


4、数据范围比较大,注意需要使用LL.....


Ac代码:


#include<stdio.h>#include<string.h>#include<map>using namespace std;#define ll __int64ll val[27];char a[1000060];ll sum[1000060];int main(){    for(int i=0;i<26;i++)    {        scanf("%I64d",&val[i]);    }    ll output=0;    scanf("%s",a);    int n=strlen(a);    for(int z=0;z<26;z++)    {        memset(sum,0,sizeof(sum));        map<ll ,ll >s;        s.clear();        for(int i=0;i<n;i++)        {            if(i==0)sum[i]=val[a[i]-'a'];            else sum[i]=sum[i-1]+val[a[i]-'a'];            if(a[i]-'a'==z)            {                output+=s[sum[i]-val[z]];                s[sum[i]]++;            }        }    }    printf("%I64d\n",output);}











0 0