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
数组代表区间
#include <iostream>
#include <cstring>
#include <cstdio>
//#pragma GCC optimize(2)
#include<time.h>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 5005
#define inf 1e18
#define eps 0.00001
typedef long long ll;
const ll mod = 1e9+7;
const double pi = acos(-1);
ll n,k = inf,m = inf,ans[maxn],arr[maxn];
ll check(ll x)
{
memset(ans,0,sizeof(ans));
ll num = 0,sum = 0;
for(ll i = 1; i + x - 1 <= n; i++)
{
if(sum%2 == 1 && arr[i] == 1)
{
ans[i] = 1;
sum++;
num++;
}
else if(sum%2 == 0 && arr[i] == 0)
{
ans[i] = 1;
sum++;
num++;
}
if(i - x >= 0)
sum -= ans[i-x+1];
}
for(ll i = n - x + 2; i <= n; i++)
{
if(sum%2 == 1 && arr[i] == 1)
{
return inf;
}
else if(sum%2 == 0 && arr[i] == 0)
{
return inf;
}
if(i - x >= 0)
sum -= ans[i-x+1];
}
return num;
}
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);cout.tie(0);
//freopen("D:\\test1.in","w",stdout);
//srand((int)time(0));
scanf("%lld",&n);
getchar();
//cin >> n;
for(ll i = 1; i <= n; i++)
{
char str;
scanf("%c",&str);
getchar();
//cin >> str;
if(str == 'F')
arr[i] = 1;
else
arr[i] = 0;
}
if(n == 1 && arr[1] == 1)
{
cout << "0 0" << endl;
return 0;
}
for(ll i = 1; i <= n; i++)
{
ll temp = check(i);
//cout << temp << endl;
if( temp < m)
{
k = i;
m = temp;
}
}
cout << k << " " << m << endl;
return 0;
}
本文探讨了一种自动牛群转向机器的最优化使用策略,旨在通过最少的操作使所有面向不一的牛统一朝向。利用算法分析了不同操作区间长度对最小化操作次数的影响,并提供了解决方案。
462

被折叠的 条评论
为什么被折叠?



