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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值