【Henu ACM Round #12 A】 Grandma Laura and Apples

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

知道题意之后就是一个模拟的过程了。 用int now记录当前苹果的个数。bool flag记录是否有小数(即半个苹果) (这样处理为了防止double精度误差 根据half 和halfplus的规则,变化now和flag即可。 变的时候把卖出去的苹果累加答案

【代码】

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

const int N  = 40;

int n,p;
string s[N+10];
ll ans = 0;

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
    cin >> n >> p;
    for (int i = 1;i <= n;i++){
        cin >> s[i];
    }
    ll now = 0,flag = 0;
    for (int i = n;i >=1;i--){
        string temp = s[i];
        if (temp!="half"){
            if (flag==0){
                flag = 1;
            }else{
                flag = 0;
                now++;
            }

            ans += 1LL*now*p;

            now*=2;
            if (flag){
                ans+=p/2;
                now++;
                flag = 0;
            }
        }else{
            ans+=1LL*now*p;
            now*=2;
            if (flag){
                ans+=p/2;
                now++;
                flag = 0;
            }
        }
    }
    cout << ans << endl;
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/8328675.html