您的位置:首页 > 其它

Educational Codeforces Round 9

2016-03-02 01:28 369 查看
Problem A

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~40

苹果单价p:2~1000

从第一次买卖开始的交易情况(half 买卖了一半数量的苹果,没有出现买卖半个苹果的情况;halfplus 买卖了一半数量的苹果,出现了买卖半个苹果的情况)

老婆婆最少总共卖了1个苹果且每次苹果都全部卖完了

输出:一共卖了多少钱 (64位)

难度:巨水(神™的花了我一个小时)

分析:最后一次交易肯定是卖了半个苹果送了半个,即剩了一个,故从剩1个苹果开始倒推。

题目问的是卖了多少钱而不是一共卖了几个苹果(不要问我为什么强调这一点!!!(神™的卡了我一个小时))

代码如下:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

int main()
{
int a,b,k;
double sum;
char c[45][12],ans[12]="halfplus";
while(cin>>a>>b)
{
for(int i=0;i<a;i++)
cin>>c[i];
k=0;
sum=1.0;
a--;
while(a--)
{
if(strcmp(c[a],ans)==0)
{
k++;
sum=sum*2+1;
}
else
sum=sum*2;
}
printf("%I64d\n",(long long int)((sum-0.5*k-0.5)*b));
}
}


Problem B

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:

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.

题意:给你一个串,每个位置都有一个字母和对应的分数,你有权利从串头或串尾开始向后或向前逐次将任意个A变成B,B变成A,求所有B对应位置的最大总分数。

输入:串长n(1~5e5)

每个位置的分数(1~1e9)

AB排序的情况;

输出:所有B对应位置的最大总分数

难度:巨水

分析:按照题意从前向后推一边找最大,在从后向前推一边找最大即可。

代码如下

#include <iostream>
#include <stdio.h>
using namespace std;

bool b[500010];
long long a[500010],ans,num1,num2;
char x[500010];
int main()
{

int n;
while(cin>>n)
{
//输入层:
for(int i=0;i<n;i++)
cin>>a[i];
scanf("%s",x);
for(int i=0;i<n;i++)
{
if(x[i]=='B')
b[i]=true;
else
b[i]=false;
}
//算法层:
long long ans=0;
//不变时的ans:
for(int i=0;i<n;i++)
if(b[i])
ans+=a[i];
num1=num2=ans;
//正推
for(int i=0;i<n;i++)
{
if(b[i])
num1-=a[i];
else
num1+=a[i];
if(num1>ans)
ans=num1;
}
//反推
for(int i=n-1;i>=0;i--)
{
if(b[i])
num2-=a[i];
else
num2+=a[i];
if(num2>ans)
ans=num2;
}
//输出层:
cout<<ans<<endl;
}
}

Problem C

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及n个串

输出:合并串

难度:基础

分析:sort的比较函数

代码如下

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
bool cmp(string a,string b)
{
return (a+b)<(b+a);
}
int main()
{
int n,i;
string s[50005];
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>s[i];
sort(s,s+n,cmp);
for(i=0;i<n;i++)
cout<<s[i];
cout<<endl;
}
}

Problem D

D. Longest Subsequence

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

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

TLE代码:

#include <iostream>
#include <string.h>
using namespace std;
int a[(int)1e6+5],b[(int)1e6+5],c[(int)1e6+5];
int main()
{
int n,m,i,ans,k,j,len;
cin>>n>>m;
for(i=0;i<n;i++)
cin>>a[i];
for(len=0,ans=j=1;j<=m;j++)
{
for(k=i=0;i<n;i++)
if(j%a[i]==0)
b[k++]=i;
if(k>len)
{
len=k;
ans=j;
for(i=0;i<len;i++)
c[i]=b[i];
}
}
cout<<ans<<' '<<len<<endl;
for(i=0;i<len;i++)
cout<<c[i]+1<<' ';
}


AC代码

#include <bits/stdc++.h>
using namespace std;

const int mm = 1000005;
int a[mm], b[mm], c[mm];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
scanf("%d", c+i);
if (c[i] <= m) ++a[c[i]];
}
for (int i = 1; i <= m; ++i)
if (a[i] != 0) {
for (int j = i; j <= m; j += i)
b[j] += a[i];
}
int ans = 1;
for (int i = 1; i <= m; i++) {
if (b[ans] < b[i]) ans = i;
}
cout << ans << " " << b[ans] << endl;
for (int i = 0; i < n; ++i)
if (ans % c[i] == 0) printf("%d ", i+1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: