Gym 100703K Word order 簡單貪心

K. Word order
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

— It is not about games, — Dragon decided that he should not tell Princess about trainings. — It is necessary to make him want to leave the Castles Valley forever.

Princess sank into reverie again. Every day Prince goes into the world outside the Valley and comes back. What if he just does not see that many things in the big world which could change his life?

— It is better say that nothing will change for him if he leaves the Valley, — Dragon broke off Princess' thoughts.

Princess thought out long before, that she would tell Prince. But Dragon, after he listened to her narration, was sure it could not make Prince want to leave the Valley. He knew Prince quite well and realized that not all of Princess' arguments be treat by Prince as "pro"s.

Let's consider that Princess' narrative is a sequence of arguments. For each argument Dragon knew, if it is "pro", "contra" or neutral.

Dragon offered Princess to modify a sequence of arguments so that the last position of "contra" precedes the first position of "pro".

Dragon was quite considerate and understood that it is difficult to Princess to significantly modify a sequence of arguments at once. For this reason he proposed her to modify a sequence step by step. In each step Princess can swap any two consecutive arguments. Of course, there was still plenty of time until evening (when Prince comes back from his work), but Dragon wants to modify a sequence with minimum number of steps.

Your task is to compute the minimum number of the steps.

Input

The first line contains integer n (2 ≤ n ≤ 100) — number of Princess' arguments.

The second line contains string of n symbols FA and N, where F corresponds to argument "pro", A corresponds to argument "contra", and N corresponds to the neutral argument.

It is guaranteed that the sequence contains at least one argument "pro" and at least one argument "contra".

Output

In the first line print the only integer — minimum number of steps, which are needed to put an original sequence in the suitable form.

Sample test(s)
input
6
FNAFNN
output
2

題目鏈接:http://codeforces.com/gym/100703/problem/K

題目大意:給定一個長度爲n的字符串,字符串僅由"F","N","A"三種字符組成,現有一種操作P,即把兩個相鄰的字符調換位置。要求把所有的A都放在所有的F左側,問需要的最少操作P的次數。

思路:首先從左至右的掃描原串,對於每一個"A",設它的左側有x個"F",則必然至少需要x次操作將"A"換到"F"的左側或者把“F”換到"A"的右側。然後對於每個"N",設它左側有L_F個"F",右側有R_A個"A",若L_F大於R_A,則更優解就是把"A"換到左側,而反之,則是把"F"換到右側。

AC代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
#define MAXN 110
char str[MAXN];
int n;
int main() {
    scanf("%d", &n);
    int ans = 0;
    scanf("%s", str + 1);
    int F_num = 0;
    for(int i = 1; i <= n; i++) {
        if(str[i] == 'F') F_num++;
        else if(str[i] == 'A') ans += F_num;
    }
    for(int i = 1; i <= n; i++) {
        if(str[i] != 'N') continue;
        int st = i, ed = i;
        while(str[ed] != 'N') ed++;
        int len = ed - st + 1;
        int L_F = 0, R_A = 0;
        for(int j = 1; j < st; j++)
            if(str[j] == 'F') L_F++;
        for(int j = ed + 1; j <= n; j++)
            if(str[j] == 'A') R_A++;
        ans += len * min(L_F, R_A);
    }
    printf("%d\n", ans);
    return 0;
}


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