POJ 3276 Face The Right Way (尺取法)

题目链接

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)

分析:
从第一个开始找如果当前为B 就需要改变 改变i到i+k-1的位置
一直找到n-k+1处 之后判断从n-k+2到n是否满足了题意 注意不要真的去修改
用一个sum值标记前面k-1个修改了多少次用来决定当前的是否要修改 利用尺取法 即前后推进

代码:

#include<stdio.h>
#include<string.h>
int a[5100],n,flag[5100];
int solve(int k)
{
    int i;
    memset(flag,0,sizeof(flag));//flag[i]表示区间[i,i+k-1] 是否需要翻转
    int sum=0,cnt=0;//前k-1个转变的次数
    for(i=1; i<=n-k+1; i++) //sum记录走到当前i,其前面k-1个翻转了多少次
    {
        if(i-k>=1)///因为每次翻转的是k个,
        {
            sum-=flag[i-k];
        }
        if(a[i]==0&&sum%2==0)///如果是B 且前面翻转了偶数次 仍旧需要翻转
        {
            flag[i]=1;
            sum+=flag[i];
            cnt++;
        }
        else if(a[i]==1&&sum%2==1)///如果是F  且前面翻转了奇数次
        {
            flag[i]=1;
            sum+=flag[i];
            cnt++;
        }
        // printf("i=%d flag[i]=%d\n",i,flag[i]);
    }

    for(i; i<=n; i++)
    {
        if(i-k>=1)
        {
            sum-=flag[i-k];
        }
        ///这就相当于还没有翻转好
        if(sum%2==0&&a[i]==0) return -1;
        else if(sum%2==1&&a[i]==1) return -1;
    }
    return cnt;
}

int main()
{
    int i,k,mn;
    char s[2];
    while(scanf("%d",&n)!=EOF)
    {
        mn=100010000;
        for(i=1; i<=n; i++)
        {
            scanf("%s",s);
            if(s[0]=='B') a[i]=0;
            else if(s[0]=='F') a[i]=1;
        }
       // for(int i=1;i<=n;i++)
         //   printf("%d",a[i]);
       // printf("\n");
        k=1;
        for(i=1; i<=n; i++)///i是指对应的间距
        {
            int mid=solve(i);///mid是指在相应的间距下的操作次数
            ///printf("k=%d,cnt=%d\n",i,mid);
            if(mid==-1) continue;///在这个间距k下不能满足情况
            if(mn>mid)
            {
                mn=mid;
                k=i;
            }
        }
        printf("%d %d\n",k,mn);
    }
    return 0;
}

转载于:https://www.cnblogs.com/cmmdc/p/7211267.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值