• 【其它算法】Face The Right Way


    Face The Right Way
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 5278   Accepted: 2462

    Description

    Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing forward, like good cows. Some of them are facing backward, though, and he needs them all to face forward to make his life perfect.

    Fortunately, FJ recently bought an automatic cow turning machine. Since he purchased the discount model, it must be irrevocably preset to turn K (1 ≤ K ≤ N) cows at once, and it can only turn cows that are all standing next to each other in line. Each time the machine is used, it reverses the facing direction of a contiguous group of K cows in the line (one cannot use it on fewer than K cows, e.g., at the either end of the line of cows). Each cow remains in the same *location* as before, but ends up facing the *opposite direction*. A cow that starts out facing forward will be turned backward by the machine and vice-versa.

    Because FJ must pick a single, never-changing value of K, please help him determine the minimum value of K that minimizes the number of operations required by the machine to make all the cows face forward. Also determine M, the minimum number of machine operations required to get all the cows facing forward using that value of K.

    Input

    Line 1: A single integer: N 
    Lines 2..N+1: Line i+1 contains a single character, F or B, indicating whether cow i is facing forward or backward.

    Output

    Line 1: Two space-separated integers: K and M

    Sample Input

    7
    B
    B
    F
    B
    F
    B
    B

    Sample Output

    3 3

    Hint

    For K = 3, the machine must be operated three times: turn cows (1,2,3), (3,4,5), and finally (5,6,7)

    Source

     
    题目大意:有N头奶牛有些朝前有些朝后。每次可以选择一段长度为K的区间翻转,问最少的翻转次数以及对应的K。
    试题分析:如何翻转?对于这个序列我们设一个f数组,当这一位与上一位不一样那么标为1,否则标为0.
         这时就有了一个很好的性质:我们翻转后中间不会变,变的只是翻转的头部和末尾+1.
         那么我们只要遇到不一样的就进行一次这样的翻转,那么N-K+2~N这一段如果有不一样的那么就没有办法了,说明我们现在枚举的K不合法。
    代码:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    inline int read(){
        int x=0,f=1;char c=getchar();
        for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
        for(;isdigit(c);c=getchar()) x=x*10+c-'0';
        return x*f;
    }
    #define LL long long
    const int MAXN=10000;
    const int INF=999999;
    char last='F';
    int N; int ansm=INF,ansk;
    int f[5001],f2[5001];
    int main(){
        N=read();
        char c;
        for(int i=1;i<=N;i++){
            cin>>c;
            if(c!=last) f[i]=1;        
            last=c;
        }
        for(int k=1;k<=N;k++){
            int cnt=0;
            for(int i=1;i<=N;i++) f2[i]=f[i];
            for(int i=1;i<=N-k+1;i++)
                if(f2[i]){f2[i+k]^=1;cnt++;}
            for(int i=N-k+2;i<=N;i++)
                if(f2[i]) {cnt=INF;break;}
            if(cnt==INF) continue;
            if(cnt<ansm){
                ansm=cnt;
                ansk=k;             
            }
        }
        printf("%d %d
    ",ansk,ansm);
    }
    

      

  • 相关阅读:
    BZOJ1029:[JSOI2007]建筑抢修(贪心,堆)
    1054. [HAOI2008]移动玩具【BFS】
    1297. [SCOI2009]迷路【矩阵乘法】
    1192. [HNOI2006]鬼谷子的钱袋【进制】
    2243. [SDOI2011]染色【树链剖分】
    1051. [HAOI2006]受欢迎的牛【强连通分量】
    codevs 2074 营救 WW
    codevs 1191 数轴染色
    codevs 2855 游乐园的迷宫 bfs
    codevs 2806 红与黑
  • 原文地址:https://www.cnblogs.com/wxjor/p/7301536.html
Copyright © 2020-2023  润新知