zoukankan      html  css  js  c++  java
  • 【其它算法】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);
    }
    

      

  • 相关阅读:
    Linux多网卡的时候执行机器Ip
    Base64加密算法
    MD5中Java和Js配套实现
    Maven依赖war开发,找不到war里头的class解决方案
    Java文件上传下载
    ①SpringBoot入门教学篇
    Java开发过程中乱码问题理解
    git切换到新的远程地址
    使用tablayout和recyclerview的时候,报重复添加Fragment错误
    项目组件化,找不到控件, or 控件为null
  • 原文地址:https://www.cnblogs.com/wxjor/p/7301536.html
Copyright © 2011-2022 走看看