poj 3276Face The Right Way—反转(开关问题)

来源:互联网 发布:电力交易中心 知乎 编辑:程序博客网 时间:2024/06/02 20:41
Face The Right Way
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3646 Accepted: 1683

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


解题思路:首先我们来看对于一个特定的k来如何求出让所有的牛朝向前方的最小操作次数。如果把牛的方向作为状态来搜索的话,那么状态拥有2^n个,我们知道对于同一个区间进行两次以上操作是多余的。与实现我们先来考虑对于最左边的一头牛,包含这头牛的区间只有一个,如果这头牛面朝前方,那么就不需要反转,如这头牛面朝后方,就必须进行翻转了,这样最左边的问题解决了,一次次这样进行下去,问题的规模逐渐减小。如果我们对所有的k来操作一次,那么最坏的情况下要操作n-k+1次,而每次操作要反转k头牛。那么解的规模是o(n^3),再优化一下区间,该题可以得解。

f[i]:区间[i,i+k-1]进行了反转的话则为1,否则为0.

再考虑第i头牛时,对其有影响的只有i-k+1到i-1头牛,通过加上所有对其有影响的牛的翻转次数,来判断和奇和偶,来确定是否反转。

#include <iostream>#include <cstdio>#include <cstring>const int maxn = 5040;int n,dir[maxn];//表示牛的方向,其中0表示F,1表示Bint f[maxn];//表示第i个牛是否进行了翻转//计算每个k的最小操作次数//如果遇到无解的情况时就返回-1int Cal(int k){    memset(f,0,sizeof(f));    int res = 0;    int sum = 0;    for(int i=0;i+k<=n;i++)    {        if((dir[i]+sum)%2 != 0)        {            res++;            f[i] = 1;        }        sum += f[i];        if(i-k+1 >= 0)            sum -= f[i-k+1];    }    //检查剩下的牛是否都朝向前方    for(int i=n-k+1;i<n;i++)    {        if((dir[i]+sum)%2!=0)            return -1;        if(i-k+1>=0)            sum-=f[i-k+1];    }    return res;}int main(){    char str[3];    scanf("%d",&n);    for(int i=0;i<n;i++){        getchar();        scanf("%s",str);        if(str[0] == 'B')            dir[i] = 1;        else            dir[i] = 0;    }    int M = n,K = 1;    for(int k=1;k<=n;k++)    {        int m = Cal(k);        if(m >= 0&& M >= m)        {            M = m;            K = k;        }    }    printf("%d %d\n",K,M);    return 0;}


1 0
原创粉丝点击