Face The Right Way POJ - 3276(反转问题,开关问题)

Face The Right Way
POJ - 3276

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)

题意:N头牛排成一列,每头牛或者向前或者向后,为了让所有牛都面向前方,农夫买了个自动转向机器,这个机器在购买时就必须设定一个数值K,机器每操作一次恰好使K头连续的牛转向,求让所有牛都能面向前方需要的最小操作次数M和对应的最小K

思路:首先我们来看看对于一个特定的K如何求出让所有牛面向前方的最小操作次数,如果把牛的方向作为状态进行搜索的话,由于状态数有2^N个,无法在时限内找出答案,那么不搜索怎么办呢?

首先,交换区间反转的顺序对结果是没有影响的,此外,对同一个区间进行两次以上的反转是多余的(这是求反转类问题的关键)由此,问题就转化成了求需要被反转的区间的集合,于是我们先考虑一下最左端的牛,包含这头牛的区间只有一个,因此如果这头牛面向前方,我们就能知道这个区间不需要反转。

反之如果这头牛面朝后方,对应的区间就必须进行反转,而且在此之后这个最左的区间就在也不需要考虑了。这样一来,通过首先考虑最左端的牛,问题规模就缩小了1,。不断地重复下去,就可以无须搜索求出最少所需的反转次数了。

此外,通过上面的分析可以知道,忽略掉对同一个区间重复反转这类多余的操作后,只要存在让所有牛都朝前的方法,那么操作就和顺序无关可以唯一确定了。

这个算法的复杂度如何呢?首先我们需要对所有K都求解一次,对于每个K,我们都要从最左端开始考虑N头牛的情况。此时最坏情况下需要进行N-K+1次反转操作,而每次反转K头牛,总的复杂度就是 O(N3) ,这样还不足以在时限内解决问题,但是区间反转部分可以优化:

令f[i]表示区间[i,i+K-1]进行了反转的话则为1,否则为0

这样,在考虑第i头牛时,如果 i1j=iK+1f[j] 为奇数的话,则这头牛的方向与起始方向是相反的否则方向不变(解释:在考虑是否反转i前,我们需要看i的方向,而i的方向收到前面能影响i的区间反转时的影响,第一个能影响i的区间是[i-K+1,i],最后一个能影响i的期间是[i-1,i-1+K],所以求和后和是奇数与起始方向相反,偶数相当于翻过去又返回来,所以不变)由于
ij=(i+1)K+1f[j]=i1j=iK+1f[j]+f[i]f[iK+1]
减去第一个区间,加上下一个区间,相当于整个区间整体后移一个位置
所以这个和每次都可以递推出来复杂度变成了 O(N2)
code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 5009;
int n;
int dir[MAXN]; //牛的方向 F:0,B:1
int f[MAXN]; //区间[i,i+K-1]是否进行反转
//固定k,求对应最小操作次数
//无解的话返回-1
int calc(int k){
    memset(f,0,sizeof(f));
    int res = 0; //操作次数
    int sum = 0; //f的和
    for(int i = 0; i + k <= n; i++){
        if((dir[i] + sum) % 2 != 0){ //判断当前是否需要反转
            res++; //如果和之前一样sum偶朝后1,奇数操作+1,如果和之前不同sum奇朝前0,奇数操作+1
            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;
}
void solve(){
    int K = 1,M = n; //初始每次操作1头牛,则需要n次操作
    //因为这里k越大,理论上如果可以使得所有牛面向前方,肯定操作次数越少,所以直接枚举
    for(int k = 1; k <= n; k++){ //枚举k
        int m = calc(k);
        if(m > 0 && M > m){
            M = m;
            K = k;
        }
    }
    printf("%d %d\n",K,M);
}
int main(){
    scanf("%d",&n);
    for(int i = 0; i < n; i++){
        char c;
        scanf(" %c",&c);
        if(c == 'F') dir[i] = 0;
        else dir[i] = 1;
    }
    solve();
    return 0;
}

from 挑战程序设计竞赛

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值