POJ3276 Face The Right Way--反转问题初学

Face The Right Way
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3061   Accepted: 1412

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)



这个题是我在初学反转问题的入门题,简单说就是N头牛排成一列,每一头牛都向前或者向后,为了让所有的牛都朝向前方,每操作一次恰好使K头连续的牛转向。求出让所有牛转向前方的操作次数M和对应的最小的K


如果我们把牛的方向作为状态进行搜索的话,由于状态数有2^N个,是无法在规定的时间内找出答案的,那么怎么办呢。首先对于反转问题,我们需要先搞清楚几个问题:

首先,交换区间反转的顺序是没有影响的,此外,对同一个区间只进行一次的反转,因为多于两次都是多余的,会出现反转0次或者1次出项的情况。谕示我们最优先考虑最左面的牛。包含这头牛的区间只有一个,因此我们可以指定到这个区间反不反转,并根据此反转,依次向后推出后面需不需要反转。

最后我们只需对小于我们所设空间的最后一部分进行判断,看其是否满足题设条件,如果满足,该方法就可行。


代码如下:

/*************************************************************************
    > File Name: Face_the_Right_Way.c
    > Author: zhanghaoran
    > Mail: 467908670@qq.com 
    > Created Time: 2015年06月04日 星期四 21时26分11秒
 ************************************************************************/

#include <stdio.h>
#include <string.h>
#define MAX_N 100010

int N;
char map[MAX_N];
int dir[MAX_N];       //牛的方向
int f[MAX_N];		  //区间[i, i + K -1]是否反转

//固定K,求对应的最小操作权限
//无解返回-1

int calc(int K){
	int res = 0;
	int sum = 0;
	int i;
	memset(f, 0, sizeof(f));
	for(i = 0; i + K <= N; i ++){
		//计算区间[i, i + K - 1]
		if((dir[i] + sum) % 2 != 0){
			//前端的牛朝向后方
			res ++;
			f[i] = 1;
		}
		sum += f[i];       
		if(i - K + 1 >= 0){
			sum -= f[i - K + 1];   //对于已经超过区间范围的之前的区域,我们将其反转情况删掉。
		}
	}

	//检查剩下的牛是否有面朝后方的情况
	for(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;
	int M = N;
	int i;
	int t;
	for(i = 1; i <= N; i ++){
		t = calc(i);
		if(t >= 0 && M > t){
			M = t;
			K = i;
		}
	}
	printf("%d %d
", K, M);
}

int main(void){
	int i;
	while(scanf("%d
", &N) != EOF){
		for(i = 0; i < N; i ++, getchar()){
			scanf("%c", &map[i]);
			if(map[i] == 'B')
				dir[i] =1;
			else dir[i] = 0;
		}
		solve();
	}
	return 0;
}
接下来会刷一道也是反转问题的稍微复杂一些的题目。

原文地址:https://www.cnblogs.com/chilumanxi/p/5136137.html