Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5024 | Accepted: 2335 |
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
Lines 2.. N+1: Line i+1 contains a single character, F or B, indicating whether cow i is facing forward or backward.
Output
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 有多个,取最小值。输出最小 m 和对应的 k。
首先观察题目发现:1.对各区间反转操作的顺序对结果没有影响 2.对一个区间反转两次就相当于没反转
满足这两个条件的,一般都可以归结为反转问题。这个问题是线性的,下一个问题则是非线性的。
从头开始往后依次考虑。考虑队首的牛,包含这头牛的区间只有一个。如果这头牛朝前,这个区间不能反转,否则这个区间必须反转。因此,第一头牛的状态就确定了这一个区间的状态。假设 k 是一个确定的值,则第1头牛反转后 k-1 头牛也跟着反转。此时二头牛的情况就变成了之前第一头牛的情况,因此1次操作问题的规模-1。然后不断循环这个过程即可。
所以,整体的思路就是从 1 到 n 枚举 k 的值,然后用上述方法检测 k 值是否合法然后更新答案。但是这样的复杂度 n(枚举k) * n(最坏要反转n - k + 1次)* n(考虑每一头牛时要反转 k头牛)。这样的时间复杂度过高,可以在反转牛的部分进行简化。
cow[i] 表示每头牛的状态,0 为朝前,1 为朝后。f[i] 表示第 i 头牛是否被反转,反转为 1, 否则为 0。考虑每一头牛时,只需看 f[i-k+1] 到 f[i-1] 相加再加上 cow[i] 的和的奇偶性。是奇数则当前牛后,需反转,f[i]=1。否则不需要反转,f[i]=0。这样只需维护一个sum就可以不用挨个反转而得到反转结果了。复杂度因此降到了 O n方。
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn = 5000 + 10;
int cow[maxn];
int f[maxn];//该牛是否反转
int n;
int Judge (int k)
{
//f[i] 和,反转次数
int sum = 0, cnt = 0;
for(int i= 0; i+k-1 < n; i++)
{
//该牛是否需要反转
if((cow[i] + sum) % 2)
f[i] = 1, cnt ++;
else f[i] = 0;
//更新sum
sum += f[i];
if(i-k+1 >= 0) sum -= f[i-k+1];
}
//对剩下的不能反转的牛判断是否合法
for(int i= n-k+1; i< n; i++)
{
if((cow[i] + sum) % 2) return -1;
if(i-k+1 >= 0) sum -= f[i-k+1];
}
return cnt;
}
int main ()
{
cin >> n;
for(int i= 0; i< n; i++)
{
char c;
scanf(" %c", &c);
//向前为 0,向后为 1
if(c == 'F') cow[i] = 0;
else cow[i] = 1;
}
//最小步数,对应 K 值
int AnsM = n, AnsK;
for(int k= 1; k<= n; k++)
{
int cnt = Judge(k);
if(cnt >= 0 && AnsM > cnt) AnsM = cnt, AnsK = k;
}
cout << AnsK << " " << AnsM << endl;
return 0;
}