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
  先我们来看对于一个特定的如何求出让所有的牛面物前方的服小慢作次数,如果把牛的方向
作为状态进搜索的话,由于状态数有2个,是无法在时限内找出答案的,那么不搜索的话
怎么办呢?
首先,交换区间反转的顺序对结果是没有影响的。此外,可以知道对同一个区间进行两次以上
的反转是多余的。由此,问题就转化成了求需要被反转的区间的集合。于是我们先考虑一下最
左端的牛包含这头牛的区间只有一个,因此如果这头牛面朝前方,我们就能知道这个区间不
需要反转,
反之,如果这头牛面朝后方,对应的区间就必须进反转了。而且在此之后这个最左的区间就再
也不需要考虑了。这样一来,通过首先考虑最左端的牛,问题的规模就缩小了1。不断地重复下
去,就可以无需搜索求出最少所需的反转次数了。
此外,通过上面的分析可以知道,忽略掉对同一个区间重复反转这类多余操作之后,只要存在让
所有的牛都朝前的方法,那么操作就和顺序无关可以唯一确定了。
这个算法的复杂度又如何呢? 首先我们需要对所有的K都求解一次,对于每个人我们都是从最在
海开始来考虑以头牛的情况。此时最坏情况下需要进行N-K+1次的反转操作,而每次操作又要反
转K头牛,于是总的复杂度就是O(N的三次方)。这样的话还不足以在时展内解决问题。但是区间翻转
还是很容易进优化的


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
int main()
{
	int n;
	cin>>n;
	char c;
	int a[10005];//F朝前为1:B朝后为0
	int f[10005]={0};//从第一头牛到 当前第 i 头牛一共转向了几次
	for(int i=1;i<=n;i++)
	{
		scanf(" %c",&c);
		a[i]=c=='F'?1:0;
	}
	int res=0xffffff,len=0;//res是最少转向次数,len是最小转向次数的K值
	for(int k=1;k<=n;k++)
	{
		int sum=0,i;//sum记录操作次数,i代表第i头牛
		for( i=1;i<=n-k+1;i++)
		{
			int t=t=i-k<0?0:i-k;//t是第一个会影响到第i头牛的牛 的下标的前一个
			if((a[i]+f[i-1]-f[t])%2==0)//这样如果第i头牛减去第t头牛+a[i]是奇数,说明第i头牛状态是朝后
			{
			  	f[i]=1+f[i-1]; //到第i头牛翻转的次数加一
			  	sum++;
			}
			else f[i]=f[i-1];//否则传递次数
		}
		bool flag=true;
		while(i<=n)//判断操作完之后是否全是F
		{
		    int t=t=i-k<0?0:i-k;
			if((a[i]+f[i-1]-f[t])%2==0)
			{
				flag=false;
				break;
			} 
			f[i]=f[i-1];
			i++;
		}
		if(flag&&res>sum)
		{	
			res=sum;
			len=k;
		}
	}
	if(res==0)
	len=0;
	cout<<len<<' '<<res;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值