POJ3276-Face The Right Wa【反转】

原题链接
Face The Right Way
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4378 Accepted: 2031
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

USACO 2007 March Gold
题目大意:有n头奶牛排成一排,有的朝前有的朝后,现在你可以使k头奶牛一次性翻转朝向(n>=k>=1),问你最少的翻转次数和此时对应的k值。
思路:对于这种交换类型的题,基本上都有一个特点那就是交换的顺序对结果没有影响,这是一个很重要的结论。然后就是对同一个区间交换两次是没有意义的,那么也就是说总共就2(n-k+1)钟情况,每种情况的区别就是区间首元素的位置。想一想,如果这个区间首元素需要进行反转我们能不对他进行反转吗?答案是不能,所以如果一个区间的首元素需要反转那么我们就一定要对他进行反转这点是毫无疑问的。那么当这个区间进行反转后我们是不是该对这个区间所有的元素进行反转呢,答案是不可以,为什么?因为复杂度这样算下来是O(n3),所以不进行反转可以吗,答案是可以的,如果我们把某个首元素之前所有的能够影响到这个元素的翻转情况全部记录下来再加上这个元素本身的翻转情况模2的结果就可以判断他是否需要反转了。而这个能够影响到这个首元素的翻转情况的记录区间是可以移动的,每一次减去这个记录区间的首元素在加上尾元素的下一个元素就可以得到下一个元素的反转状态了。,这样时间就节省到了O(n^2)了就可做了

//http://poj.org/problem?id=3276
#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = (~(0ULL)>>1);
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=5000 + 5;

int dir[maxn],f[maxn],n;

//得到如果一次旋转k个相邻的单位需要旋转的最少的次数
int solve(int k){
	memset(f,0,sizeof(f));
	int sum=0,res=0;//这里的sum是一个点之前的k-1长度的区间的反转次数的和
	for(int i=0;i+k<=n;i++){//i+k<=n是因为i可以到n-1,k最小都是1
		if((dir[i] + sum) %2== 1){//如果当前该奶牛需要反转
			f[i]=1;
			res++;//反转的次数此时就要+1
		}
		sum+=f[i];//加上后面的影响的部分
		if(i-k+1>=0) sum-=f[i-k+1];//减去前面的影响的部分
	}
	//检查下后面的转k个照顾不到的地方有没有不能反转的
	for(int i=n-k+1;i<n;i++){//从n-k+1开始是因为最后剩下的区间长度就是k-1了
		if((dir[i] + sum) %2== 1) return -1;//无解
		if(i-k+1>=0) sum-=f[i-k+1];
	}
	return res;
}

int main(){
		memset(dir,0,sizeof(dir));
		cin >> n;
		char t;
		for(int i=0;i<n;i++){
			cin >> t;
			if(t=='B') dir[i]=1;
		}
		int mink=1,minres=n;//设置成最坏的情况
		for(int k=1;k<=n;k++){//列举出所有可能的k
			int tres=solve(k);
			if( tres >=0 && tres<minres){
				minres=tres;
				mink=k;
			}
		}
		printf("%d %d\n",mink,minres);
        return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门豪杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值