题意:
N头牛站成一条线,分别朝向前后两个方向,机器可以使连续K头牛同时改变方向,要求所有牛最终朝向前方,问机器操作次数的最小值及此时的最小K值。
分析:
第一眼看感觉是二分搜索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.
是在最小操作的基础上的最小K值,而操作数可达
直接暴力的话时间复杂度
因为是K个连续的一起转,即第
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
using namespace std;
const int maxn = 5005, INF =0x3fffffff;
int s[maxn],a[maxn];
int N;
int judge(int k)
{
memset(a, 0, sizeof(a));
int cnt = 0;
int sum = 0;
for(int i = 0; i < N - k + 1; i++){
if((s[i] + sum) % 2 == 1){
a[i] = 1;
cnt++;
}
sum += a[i];
if(i-k+1>=0) sum -= a[i-k+1];
}
for(int i = N - k +1; i < N; i++){
if((s[i] + sum) % 2 == 1){
return -1;
}
if(i-k+1>=0) sum -= a[i-k+1];
}
return cnt;
}
int main (void)
{
map<char,int>m;
m.insert(make_pair('B',1));
m.insert(make_pair('F',0));
scanf("%d", &N);
char c;
for(int i = 0; i < N;i++){
getchar();
c = getchar();
s[i] = m[c];
}
int res = INF, k;
for(int i = 1; i <= N;i++){
int ans = judge(i);
if(ans == -1) continue;
else if(ans < res){
res = ans;
k = i;
}
}
printf("%d %d
", k, res);
}