POJ 3276 —— 开关问题

Face The Right Way
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1929 Accepted: 908

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 ofK.

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

题意是给你n头牛,并告诉你他们头的方向:F代表向前,B代表向后。你有一种能力,就是把区间为k的牛全部翻转,请你输出使得全部牛头朝前的最小翻转次数的次数和此时的K。

思路就是类似关灯问题,我将一头牛翻转偶数次相当于没翻转,翻转奇数次相当于翻一次,这样我就可以通过统计该点目前翻转的次数来判断牛的朝向。于是我们可以枚举K,然后判断K是否符合情况,因为K越大翻转次数一定最小,所以我们扫一遍K就可得到结果。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int n , dir[MAXN] , f[MAXN];
int calc(int k)
{
    clr(f ,0 );
    int res = 0 , sum = 0;
    for(int i = 0 ; i + k <= n ; i++)
    {
        if((dir[i] + sum) & 1)
        {
            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) & 1)
        {
            return -1;
        }
        if(i - k + 1 >= 0)
        {
            sum -= f[i - k + 1];
        }
    }
    return res;
}
void solve()
{
    int K = 1 , M = n;
    FORR(k , 1 , n)
    {
        int m = calc(k);
        if(m >= 0 && M > m)
        {
            M = m;
            K = k;
        }
    }
    printf("%d %d\n" , K , M);
}
int main()
{
    char str[5];
    while(~scanf("%d" ,&n))
    {
        for(int i = 0 ; i < n;  i++)
        {
            scanf("%s" , str);
            dir[i] = str[0] == 'B';
        }
//        FOR(i ,0 , n)cout << dir[i] << endl;
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值