CF GYM 100703K Word order

题意:给一个字符串,其中只有F、A、N三种字母,问最少交换多少次能使所有的A在所有F之前。

解法:贪心。先预处理每位的左边有多少F右边有多少A,对于每位A必须至少向左交换的次数为它左面的F个数,而对于N来说比较左面F的个数和右面A的个数,将少的部分交换至N的另一端。将以上两种情况的答案加和即为最后答案。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int main()
{
    int n;
    string s;
    while(~scanf("%d", &n))
    {
        cin >> s;
        int a[105] = {0}, f[105] = {0};
        if(s[0] == ‘F‘)
            f[0] = 1;
        int ans = 0;
        for(int i = 1; i < n; i++)
        {
            if(s[i] == ‘F‘)
                f[i] = f[i - 1] + 1;
            else
                f[i] = f[i - 1];
        }
        for(int i = n - 1; i >= 0; i--)
        {
            if(s[i] == ‘A‘)
            {
                a[i] = a[i + 1] + 1;
                ans += f[i];
            }
            else
                a[i] = a[i + 1];
        }
        for(int i = 0; i < n; i++)
        {
            if(s[i] == ‘N‘)
                ans += min(f[i], a[i]);
        }
        printf("%d\n", ans);
    }
    return 0;
}

  

时间: 2024-08-27 00:06:24

CF GYM 100703K Word order的相关文章

Gym 100703K Word order 贪心

题目链接 题目大意:给定一个长度为n的字符串,字符串仅由"F","N","A"三种字符组成,现有一种操作P,即把两个相邻的字符调换位置.要求把所有的A都放在所有的F左侧,问需要的最少操作P的次数. 思路:首先从左至右的扫描原串,对于每一个"A",设它的左侧有x个"F",则必然至少需要x次操作将"A"换到"F"的左侧或者把“F”换到"A"的右侧.然后

CF gym 101933 K King&#39;s Colors —— 二项式反演

题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \),设为 \( f(i) \),设恰好 i 种颜色为 \( g(i) \) 那么 \( f(i) = \sum\limits_{j=0}^{i} C_{i}^{j} * g(j) \) 二项式反演得到 \( g(i) = \sum\limits_{j=0}^{k} (-1)^{k-j} * C_{k}

cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

题目: Description standard input/output As most of you know, the Arab Academy for Science and Technology and Maritime Transport in Alexandria, Egypt, hosts the ACPC Headquarters in the Regional Informatics Center (RIC), and it has been supporting our r

cf gym 100960 G. Youngling Tournament set+树状数组

G. Youngling Tournament time limit per test 2 seconds memory limit per test 256 mebibytes input standard input output standard output Yoda, the Grand Master of the Jedi Order, hit on the idea to hold a tournament among younglings. He has not chosen t

Gym 100431E Word Cover 题解:KMP神用

题意: 给你一个串,问你他的每个前缀的最小重复单元,其中单元是可以重叠的,最后按顺序输出即可.比如样例中abaabaa的最小重复单元为abaa,所以相应输出为4. 样例: input : abaabaababa outpit:1 2 3 4 5 3 4 5 3 10 3 kmp过程就不用多说了,现在我们利用next数组的性质来对问题进行求解. 我们首先用一个ans[maxn]数组来记录最后的答案,且我们的字符串下标从0开始,显然,我们ans[i]的最大值为i+1,我们因此也用此值对其进行初始化.

CF Gym 101955G Best ACMer Solves the Hardest Problem

链接:https://codeforces.com/gym/101955/problem/G 题意:在二维平面上四种操作: 1,加一个带权的点: 2,删去一个点: 3,给一个点周围欧几里得距离为sqrt(k)的存在的点点权都加w: 4,查询一个到点欧几里得距离为sqrtk的点权和. x, y<6000, k<1e7, sigma(询问次数)<1e6,time:12s 题解:原本以为是数据结构,发现距离为k的x,y其实不多,直接存vector<pii>dis[maxk]暴力即可

CF Gym 102059E Electronic Circuit (set存图删点)

链接:https://codeforces.com/gym/102059/problem/E 题意:n个点, m条线,问这个电路是否合法,合法:可以确定一个起点和一个终点. 题解:不断的删点,删除度数为2的点,再相连,看最终度数为1的点的个数是否为2.set存图 #include <bits/stdc++.h> using namespace std; const int maxn=3e5+5; set<int> S[maxn]; int n, m; void del_edge()

CF Gym 100187A Potion of Immortality

根据兔子试药情况可以缩小范围,如果死了,不在试过的药里面,如果活着,在试过的药里. 最糟的情况: 两个原则 1.能确定药所在的范围的尽量大,2.死得兔子尽量多. 如果当前不知道情况的药n为k的二倍以上,那么基于上面两个原则,试过药的兔子肯定会死. 没死:范围k,损失的兔子0 死了:范围n-k,损失的兔子1 (n>2*k) 设r=n%k,经过上述过程,损失了n/k-1只兔子,转移到了当前状态范围w = k+r, 1.r == 0 那么可以补充一个毒药,变成w=k+1,根据鸽巢原理再死一个就可以确定

CF Gym 100500A Poetry Challenge

题解:暴力dfs,如果有一个选择,能让对手必败,那么就是必胜态,能转移到的状态都是对手的必胜态,或者无法转移,就是必败态. 总算是过了,TLE是因为状态没判重,不过我还是有一点没想明白,为什么状态会出现重复 #include<cstdio> #include<cmath> #include<vector> #include<map> #include<set> #include<algorithm> #include<iostr