BNU 26476 Doorman【NC】

鏈接:


Doorman

1000ms
65536KB
64-bit integer IO format: %lld      Java class name: Main
Font Size:  
Type:  

The doorman Bruno at the popular night club Heaven is having a hard time fulfilling his duties. He was told by the owner that when the club is full, the number of women and men let into the club should be roughly the same. When the night club opens, people wanting to enter the club are already lined up in a queue, and Bruno can only let them in one-by-one. He lets them in more-or-less in the order they are lined up. He can however decide to let the second person in the queue cut the line and slip into the club before the person in front. This will no doubt upset the person first in line, especially when this happens multiple times, but Bruno is quite a big guy and is capable of handling troublemakers. Unfortunately though, he is not that strong on mental calculations under these circumstances. He finds keeping track of the difference of the number of women and number of men let into the club a challenging task. As soon as the absolute difference gets too big, he looses track of his counting and must declare to the party people remaining in the queue that the club is full.

Input

The first line of input contains a positive integer X<100 describing the largest absolute difference between the number of women and number of men let into the club, that Bruno can handle. The second line contains a string consisting solely of the characters ’W’ and ’M’ of length at most 100, describing the genders of the people in the queue, in order. The leftmost character of the string is the gender of the person first in line.

Output

The maximum number of people Bruno can let into the club without loosing track of his counting. You may assume that the club is large enough to hold all the people in the queue.

Sample Input

1
MWWMWMMWM

Sample Output

9

Source



題意:


      第一行 N 表示能夠容忍的不能配對的人數
     第二行一條 100 個字符以內的字符串'W'代表女人, 'M'代表男人


      看門人一次只讓一個人進去,男女儘量配對,如果不配對的人數超過了 N
      那麼看們人就分不清了,此時他就會宣佈俱樂部人滿,不再讓後面的人進入
      輸出能進去的最多的人數。


思路:


每次進入一個人就判斷一次男女不配對的人數,直到不合法。

注意:關於分不清的時候的跳出判斷情況。
      要一直判斷到男女之差 > N+1
      因爲即便是最多隻能容許 N 個人沒有配對,查找到了第 N+1 個人不能配對
      那麼如果查到下一個人可以和前面的配對,那麼就可以新增加兩個人


      也就是說:
      2
      MMMW 的結果是 4

總結:很水的一題了,比賽的時候開始沒看清楚題意,見到過的人比較多,隨便YY了下 WA
      然後再看題,但是到了男女只差正好是 N+1 的時候跳出判斷的 WA。
      然後懶得搞了,繼續看其他的題目,Orc 一個人一直死磕這題。
      最後我又返回來寫這題,讓 Orc 找了組數據,測試過了,然後我說交了啊!Orc 絕望的同意了,當時差不多所有的隊伍都過了
      吧。
      然後終於 AC 了,但是此時已經過了兩個小時了。簡單的題目做的慢,難的題目搞不出,最終慘淡的逃不過被虐的命運。。。

我想着等到正式比賽的時候,應該對於難的題目,在那種壓力下,就算是那種算法我們平時有過研究,但是應該也無法快速準確的寫出來,所以關鍵就在於快速準確的 AC 簡單的題目了。

code:

/**
題意:第一行 N 表示能夠容忍的不能配對的人數
      第二行一條 100 個字符以內的字符串'W'代表女人, 'M'代表男人

      看門人一次只讓一個人進去,男女儘量配對,如果不配對的人數超過了 N
      那麼看們人就分不清了,此時他就會宣佈俱樂部人滿,不再讓後面的人進入
      輸出能進去的最多的人數。

      注意:關於分不清的時候的跳出判斷情況。
            要一直判斷到男女之差 > N+1
            因爲即便是最多隻能容許 N 個人沒有配對,查找到了第 N+1 個人不能配對
            那麼如果查到下一個人可以和前面的配對,那麼就可以新增加兩個人

            也就是說:
            2
            MMMW 的結果是 4

*/
#include<string>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

char str[110];
int n;
int main()
{
    while(scanf("%d", &n) != EOF)
    {
        scanf("%s", &str);
        int n1;
        int n2;
        int ans = 0;
        n1 = n2 = 0;
        int len = strlen(str);
        int flag = 0;
        int Min, Max;
        for(int i = 0; i < len; i++)
        {
            if(str[i] == 'M') n1++;
            if(str[i] == 'W') n2++;
            Min = min(n1, n2); //依次記錄一次
            Max = max(n1, n2);
            if(Max-Min > (n+1)) //如果男女之差比能准許的 N 要大 2,那麼守門人就分不清了,馬上宣佈人滿,不管後面排隊的
            {
                flag = 1; //標記
                break;
            }
        }
        if(flag == 0 && Max-Min <= n)  //如果沒有標記,全部排隊的人數也合法:男女之差不超過 N
            ans = len;
        else //如果標記了,最大限度的讓人進去,男女之差正好爲 N
            ans = Min*2+n; //當然也包含了沒有標記,男女之差 > N 的情況
        printf("%d\n", ans);
    }
    return 0;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章