POJ 3276 Face The Right Way

Face The Right Way
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5259 Accepted: 2454

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
题目链接:http://poj.org/problem?id=3276

题意:有N头牛,最初它们的朝向不一致,为使得它们都朝向前方,现有一台转向器,设定K值,可使得相邻的K头牛转变朝向,问需要的最小转向次数和最小的K。

解题思路:如果对于一个特定的K来求转向次数,那么对于左边第一头牛,如果朝向是朝后,那么就必须使用一次转向操作,将相邻的K头牛转变朝向,接下来就是第二头牛作为左边第一头牛,这样就可以一步一步缩小范围,但是这样的话,时间复杂度为N的3次方,不能满足题目的要求,但是有这样的思路之后,对于区间内的反转可以定义一个数组记录状态从而可以在常数的时间内完成。这样的话时间复杂度就变为了N的2次方,可以在时限内完成。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 5000 + 10;
int N;
int dir[MAXN];//牛的朝向 
int f[MAXN];//区间[i,i+K-1]是否反转 
int calc(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;//如果有朝向后方的就返回-1,说明此时的k不能将所有的牛都朝向前方 
		if(i-k+1>=0) sum-=f[i-k+1];
	}
	return res;
}
void solve()
{
	int K=1,M=N;
	for(int k=1;k<=N;k++)
	{
		int m=calc(k);//固定k求操作数m 
		if(m>=0&&M>m)
		{
			M=m;
			K=k;
		}
	}
	printf("%d %d\n",K,M);
}
int main(void)
{
	char ch;
	scanf("%d",&N);
	for(int i=0;i<N;i++)
	{
		getchar();
		ch=getchar();
		dir[i]=ch=='F'?0:1;//0表示牛的朝向为朝前方,1则表示牛的朝向为超后方 
	}
	solve();
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值