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.
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.
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.
2 10
half
halfplus
15
3 10
halfplus
halfplus
halfplus
55
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.
#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;
}
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.
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).
Print the only integer a — the maximum strength Bob can achieve.
5
1 2 3 4 5
ABABA
11
5
1 2 3 4 5
AAAAA
15
1
1
B
1
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.
#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;
}
题意:把n个字符串组成一个,求组成的字符串最小是多少,输出结果
#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;
}