GYM 100523H

Afternoon Tea HER
Available memory: 32MB.
During his visit at Bytic Islands Byteasar really enjoyed the national beverage of Byteans, that is, tea with milk. This drink is always prepared in a strictly determined manner, which is as follows. Firstly the teacup is filled with tea mixed half and half with milk. Then, an n-letter ceremonial word consisting of letters H and M is chosen. Now, for i = 1;2;:::;n, the following action is performed: if the i-th letter of the ceremonial word is H, one should drink half of the teacup, add tea until the teacup is full and stir. On the other hand, if the i-th letter of the word is M, one should perform a similar action, however milk should be added instead of tea. After such action is performed for each letter of the ceremonial word, the remaining liquid is disposed of. Each time Byteasar performs the ceremony, he wonders which of the ingredients he has drunk more: tea or milk. Help Byteasar answer this question.
Input
The first line of input holds an integer n (1 ¬ n ¬ 100000). The second line contains an n-letter word consisting of letters H and M; this is the ceremonial word used by Byteasar.
Output
Your program should output a single letter H if Byteasar has drunk more tea than milk; a single letter M if he has drunk more milk than tea; or the word HM if he has drunk equal amounts of tea and milk.
Example
For the input data:
5
HMHHM
the correct result is:
H
Explanation of the example: Byteasar has drunk 137 64 teacups of tea and
59 64 teacups of milk in total.


题意:

有个杯子一开始装了一半茶,一半牛奶,给出一个字符串str,每次看字符串的第i位如果是H就把杯子里的水喝掉一半,在添满茶,如果是M就喝掉一半,添满牛奶。

问到最后是喝的牛奶多还是茶多。

思路:

设H个数为n,M个数为m,最后剩的茶数量为x杯,牛奶数为y杯,
只需判断(n+1)/2-x 是否大于 (m+1)/2-y
也就是(n-m)-2*(x-y)是否大于0
然后n-m为整数,只要判断x-y是在[-1, -1/2]还是[-1/2, 0],还是[0, 1/2],还是[1/2, 1]之间就能判断上式是否成立;

如果最后两个字母是HH则说明x-y位于 [1/2, 1]之间, 如果说MH,则位于[0, 1/2]之间,MM和HM依此类推,于是问题就解决了。


事实上最后一个字母是没用的,最后一步喝掉一半水之后再添上的水一定会剩下来。

所以其实只要统计前n-1个字母就行了。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>

using namespace std;

#define INF 1000000000
#define N 1000010
#define ll long long

char str[N];
int main()
{
    //freopen("C:\\Users\\zfh\\Desktop\\in.txt", "r", stdin);
    int n, m, k;
    while(scanf("%d", &k) != -1)
    {
        scanf(" %s", str);
        n = m = 0;
        if(k == 1)
        {
            printf("HM\n");
            continue;
        }
        int add = 0;
        for(int i = 0; i < k; i++)
        {
            if(str[i] == 'H') n++;
            else m++;
        }
        if(str[k-2] == 'H' && str[k-1] == 'H') add = -2;
        else if(str[k-2] == 'M' && str[k-1] == 'H') add = -1;
        else if(str[k-2] == 'M' && str[k-1] == 'M') add = 2;
        else add = 1;

        if(add > 0)
        {
            if((n-m)+add > 0)
                printf("H\n");
            else printf("M\n");
        }
        else
        {
            if((n-m)+add >= 0)
                printf("H\n");
            else printf("M\n");
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值