题意:有n个顾客买苹果,每个苹果p元half就是这个顾客买了一半的苹果halfplus就是这个顾客买了一半苹果,最后还送了他半个苹果最后恰好卖完所有苹果,问你赚了多少钱
思路:倒着做就好了
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 100000
#define LL long long
int cas=1,T;
LL ans = 0;
string s[50];
int main()
{
int n,p;
scanf("%d%d",&n,&p);
for (int i = 0;i<n;i++)
cin >> s[i];
LL now = 0;
for (int i = n-1;i>=0;i--)
{
if (s[i]=="half")
{
ans+=now*p;
now*=2;
}
else
{
ans+=p/2+now*p;
now=now*2+1;
}
}
cout << ans << endl;
//freopen("in","r",stdin);
//scanf("%d",&T);
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
Description
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
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
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
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
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
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
Sample Input
Input
2 10 half halfplus
Output
15
Input
3 10 halfplus halfplus halfplus
Output
55