Educational Codeforces Round 9 A B C D F

链接:戳这里

A. Grandma Laura and Apples
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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和苹果的价格p,每个客人给出half或halfplus  分别代表偶数一半和奇数一半。


思路:halfplus 肯定是带0.5的  half 也就是一半   从后往前模拟就可以了。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF (1ll<<60)-1
using namespace std;
string s[55];
int n,p;
int main(){
    scanf("%d%d",&n,&p);
    for(int i=0;i<n;i++) cin>>s[i];
    ll ans=0;
    double x=0;
    for(int i=n-1;i>=0;i--){
        if(s[i]=="halfplus"){
            ans+=(ll)p*(x+0.5);
            x=(x+0.5)*2;
        } else{
            ans+=(ll)p*x;
            x=x*2;
        }
        ///printf("%f %I64d\n",x,ans);
    }
    printf("%I64d\n",ans);
    return 0;
}



B. Alice, Bob, Two Teams
time limit per test1.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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:

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



题意:Alice 和 Bob 玩一个游戏,游戏规则如下

1:Alice 分一段长为n*pi (1<=i<=n) 的字符串,仅有A B 两种字母,每个字母连续出现pi次

2:Bob 可以翻转最多一次该字符串的其中一段前缀或一段后缀,翻转的意思是把一段区间内的A->B或B->A

3:Alice得到A,Bob得到B。

计算出Bob最多可以得到多少B。


思路:数组记录前缀和,计算出当前i之前有多少A和B,当前i之后有多少A和B

然后枚举每个i的位置  翻转当前i的前缀或者后缀,记录max。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
ll a[500100],A[500100],B[500100];
char s[500100];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
    scanf("%s",s+1);
    A[0]=B[0]=0;
    for(int i=1;i<=n;i++){
        if(s[i]=='A') {
            A[i]=A[i-1]+a[i];
            B[i]=B[i-1];
        }
        else {
            B[i]=B[i-1]+a[i];
            A[i]=A[i-1];
        }
    }
    ll ans=max(B[n],A[n]);
    for(int i=1;i<=n;i++){
        ll x=A[n]-A[i-1];
        ll y=B[n]-B[i-1];
        ans=max(ans,B[i-1]+x);
        ans=max(ans,A[i-1]+y);
    }
    printf("%I64d\n",ans);
    return 0;
}
/*
5
6 1 1 1 1
BAAAA
*/



C. The Smallest String Concatenation
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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个字符串,输出这n个字符串组合之后字典序最小的字符串

思路:字符串可以直接比较大小,所以只需要判断a+b<b+a就可以了

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
string s[50010];
bool cmp(string a,string b){
    return (a+b)<(b+a);
}
int main(){
    scanf("%d\n",&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];
    return 0;
}


D. Longest Subsequence
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≤ m.

A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements.

The LCM of an empty array equals 1.

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 106) — the size of the array a and the parameter from the problem statement.

The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of a.

Output
In the first line print two integers l and kmax (1 ≤ l ≤ m, 0 ≤ kmax ≤ n) — the value of LCM and the number of elements in optimal subsequence.

In the second line print kmax integers — the positions of the elements from the optimal subsequence in the ascending order.

Note that you can find and print any subsequence with the maximum length.

Examples
input
7 8
6 2 9 2 7 2 3

output
6 5
1 2 4 6 7

input
6 4
2 2 2 3 3 3

output
2 3
1 2 3

题意:给定n个数 和m 要求满足a的最多个数子序列的LCM<=m    输出子序列的个数以及这段子序列的LCM

思路:(1<=n,m<=1e6) 所以直接枚举(1~1e6)每个数处于LCM的位置下  有多少answer
dp[i]记录可以整除i的数的作为最小公倍数的数量,处理下细节就可以了 O(mlogm+n)

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
int a[1000100];
int dp[1000100];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        if(a[i]<=m) dp[a[i]]++;
    }
    for(int i=m;i>=1;i--){
        if(dp[i]){
            for(int j=i*2;j<=m;j+=i){
                dp[j]+=dp[i];
            }
        }
    }
    int num=0,ans=0;
    for(int i=1;i<=m;i++){
        if(dp[i]>num) {
            num=dp[i];
            ans=i;
        }
    }
    if(!num) cout<<1<<" "<<0<<endl;
    else {
        cout<<ans<<" "<<num<<endl;
        for(int i=1;i<=n;i++){
            if(ans%a[i]==0) cout<<i<<" ";
        }
        cout<<endl;
    }
    return 0;
}


F. Magic Matrix
time limit per test5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
You're given a matrix A of size n × n.

Let's call the matrix with nonnegative elements magic if it is symmetric (so aij = aji), aii = 0 and aij ≤ max(aik, ajk) for all triples i, j, k. Note that i, j, k do not need to be distinct.

Determine if the matrix is magic.

As the input/output can reach very huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

Input
The first line contains integer n (1 ≤ n ≤ 2500) — the size of the matrix A.

Each of the next n lines contains n integers aij (0 ≤ aij < 109) — the elements of the matrix A.

Note that the given matrix not necessarily is symmetric and can be arbitrary.

Output
Print ''MAGIC" (without quotes) if the given matrix A is magic. Otherwise print ''NOT MAGIC".

Examples

input
3
0 1 2
1 0 2
2 2 0

output
MAGIC

input
2
0 1
2 3

output
NOT MAGIC

input
4
0 1 2 3
1 0 3 4
2 3 0 5
3 4 5 0

output
NOT MAGIC


题意:给定n*n的矩阵,矩阵需要满足 aii==0 && aij==aji && aij<=max(aik,ajk) 对于任意的i,j,k(可以相同)

思路:第一和第二个条件很好处理,问题就是在处理第三个条件。
首先对于任意的i,j,k  aij<=max(aik,ajk) 我们把它看成n个点,aij表示i->j的距离 ,那么我们需要知道在i到j的任意路径上的最大值需要大于等于aij  ,也就是aij必须是要小于取任意i到j的路径上的最大值
仔细想想,我们跑最小生成树,如果aij的权值> i到j路径上的任意值的话  那就是not magic  反之magic
我们可以按边的大小排个序,用并查集存当前最小边的点,当前判断ij是否已经在集合中,在的话说明aij这条边有问题,也就是aij的权值大于i到j路径上的任意值了,那么答案就出来了。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
struct edge{
    int u,v,w,next;
}e[2501*2501];
int head[2501],a[2501][2501],fa[2501];
int n,tot=0;
void add(int u,int v,int w){
    e[tot].u=u;
    e[tot].v=v;
    e[tot].w=w;
    e[tot].next=head[u];
    head[u]=tot++;
}
bool cmp(edge a,edge b){
    return a.w<b.w;
}
int find(int x){
    if(x!=fa[x]) fa[x]=find(fa[x]);
    return fa[x];
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        fa[i]=i;
        head[i]=-1;
        for(int j=1;j<=n;j++) scanf("%d",&a[i][j]);
    }
    int flag=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j && a[i][j]) flag=1;
            if(a[i][j]!=a[j][i]) flag=1;
            if(i>j){
                add(i,j,a[i][j]);
                add(j,i,a[j][i]);
            }
        }
    }
    if(flag) cout<<"NOT MAGIC"<<endl;
    else {
        sort(e,e+tot,cmp);
        int last=0;
        for(int i=0;i<tot;i++){
            if(i+1<tot && e[i].w==e[i+1].w) continue;
            for(int j=last;j<=i;j++){
                int xx=find(e[j].u);
                int yy=find(e[j].v);
                if(xx==yy) {
                    cout<<"NOT MAGIC"<<endl;
                    return 0;
                }
            }
            for(int j=last;j<=i;j++){
                int xx=find(e[j].u);
                int yy=find(e[j].v);
                fa[xx]=yy;
            }
            last=i+1;
        }
        cout<<"MAGIC"<<endl;
    }
    return 0;
}


你可能感兴趣的:(Educational Codeforces Round 9 A B C D F)