Educational Codeforces Round 9 总结

A. Grandma Laura and Apples
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.

She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.

So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).

For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).

Print the total money grandma should have at the end of the day to check if some buyers cheated her.

Input

The first line contains two integers n and p (1 ≤ n ≤ 40, 2 ≤ p ≤ 1000) — the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.

The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.

It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.

Output

Print the only integer a — the total money grandma should have at the end of the day.

Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

Examples
input
2 10
half
halfplus
output
15
input
3 10
halfplus
halfplus
halfplus
output
55
Note

In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.

题意:一个老太太,拉了不知道多少个苹果去市场,然后n个人,每个人不是买了一半,就是买了一半+老太太送了半个,每个人买走的每个苹果都是整个的。然后,神奇的是,老太太把苹果最后买完了
解:从下往上求,最后一个绝对的1.
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL __int64
const int maxm=1e3+10;
char s[maxm][10];
int main()
{
    LL n,p;
    while(scanf("%I64d%I64d",&n,&p)!=EOF)
    {
        double cnt=0;
        p=(double)p;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            if(strcmp(s[i],"halfplus")==0)
            {
                cnt+=0.5;
            }
        }
        double sum=1.0;
        for(LL i=n-2;i>=0;i--)
        {
            if(strcmp(s[i],"halfplus")==0)
            {
                sum=(sum+0.5)*2.0;
            }
            else
            {
                sum=sum*2.0;
            }
        }
        double x=(sum-cnt)*p;
        printf("%0.0lf\n",x);
    }
    return 0;
}
B. Alice, Bob, Two Teams
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi.

The way to split up game pieces is split into several steps:

  1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B.
  2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once.
  3. Alice will get all the pieces marked A and Bob will get all the pieces marked B.

The strength of a player is then the sum of strengths of the pieces in the group.

Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.

Input

The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces.

The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.

The third line contains n characters A or B — the assignment of teams after the first step (after Alice's step).

Output

Print the only integer a — the maximum strength Bob can achieve.

Examples
input
5
1 2 3 4 5
ABABA
output
11
input
5
1 2 3 4 5
AAAAA
output
15
input
1
1
B
output
1
Note

In the first sample Bob should flip the suffix of length one.

In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.

In the third sample Bob should do nothing.

题意:Bob可以选前缀或者后缀,翻转这个所选区间的后缀。求两个队的strength最大值是多少
解:可以采用dpA,dpB,分别记录两个队的前缀和。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL __int64
const LL maxm=1e6+10;
LL p[maxm];
char s[maxm];
LL dpA[maxm];
LL dpB[maxm];
int main()
{
    LL n;
    while(scanf("%I64d",&n)!=EOF)
    {
        memset(dpA,0,sizeof(dpA));
        memset(dpB,0,sizeof(dpB));
        LL sumA=0;
        LL sumB=0;
        for(LL i=0;i<n;i++)
        {
            scanf("%I64d",&p[i]);
        }
        scanf("%s",s);
        int len=strlen(s);
        for(LL i=0;i<len;i++)
        {
            if(s[i]=='B')
            {
                dpB[i]=dpB[i-1]+p[i];
                sumB+=p[i];
                dpA[i]=dpA[i-1];
            }
            else
            {
                dpB[i]=dpB[i-1];
                sumA+=p[i];
                dpA[i]=dpA[i-1]+p[i];
            }
        }
        sumB=max(sumA,sumB);
        for(LL i=0;i<len;i++)
        {
            sumB=max(sumB,dpB[i]+dpA[len-1]-dpA[i]);
        }
        for(LL i=0;i<len;i++)
        {
            sumB=max(sumB,dpA[i]+dpB[len-1]-dpB[i]);
        }
        printf("%I64d\n",sumB);
    }
    return 0;
}

C. The Smallest String Concatenation
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them together in some order such that the resulting string would be lexicographically smallest.

Given the list of strings, output the lexicographically smallest concatenation.

Input

The first line contains integer n — the number of strings (1 ≤ n ≤ 5·104).

Each of the next n lines contains one string ai (1 ≤ |ai| ≤ 50) consisting of only lowercase English letters. The sum of string lengths will not exceed 5·104.

Output

Print the only string a — the lexicographically smallest string concatenation.

Examples
input
4
abba
abacaba
bcd
er
output
abacabaabbabcder
input
5
x
xx
xxa
xxaa
xxaaa
output
xxaaaxxaaxxaxxx
input
3
c
cb
cba
output
cbacbc


题意:把n个字符串组成一个,求组成的字符串最小是多少,输出结果
解:简单的sort排序
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxm=1e5+10;
string s[maxm];
char cmp(string a,string b)
{
    return (a+b)<(b+a);
}
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>s[i];
        }
        sort(s,s+n,cmp);
        for(int i=0;i<n;i++)
        {
            cout<<s[i];
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(Educational Codeforces Round 9 总结)