Face The Right Way
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6707   Accepted: 3123

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)
 
题意:N头牛排成了一排,每头牛都超前或者朝后。为了让所有的牛都头朝前需要一个机器,这个机器可以使得连续的K头牛反转(超前变朝后,朝后变朝前),需要反转M次,求最少的操作次数M和对应的最小值K
思路:显然,一头牛如果反转两次就等于没有反转,所以一头牛要是被反转两次就多余了。一排牛,我们从左往右看的话,如果第一头牛是头朝后的,那么势必这头牛需要被反转,如果这头牛是头朝前的,那么我们就可以从第二头牛开始进行反转如果第二头牛它是头朝后的话,比如如果一排牛是BBFBFBB(F向前,B向后),那么我们从左往右看第一头牛它是朝后的,所以需要反转,我们假设此时的K是3,那么反转后是FFBBFBB,由于我们是为了第一头牛朝前,那么反转一次后第一头牛肯定朝前了,接下去我们发现第二头牛头刚好也是朝前,那么我们就继续往下看第三头,第三头头往后,那么就反转345号牛,结果为FFFFBBB,此时,我们去找第四头牛时发现他也是朝前的,那就继续往下,第五头牛朝后,那么就把567给反转。但最后一次反转后要记得验证是否最后一次使得接下去的K个牛都是头朝前的。而K的找寻这里用了暴力,1-N循环了一遍。
 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
typedef long long ll;
const int maxn = 5e3+;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
} int N,M;
int dir[maxn],f[maxn]; //牛的方向 F:0 B:1 int calc(int K)
{
memset(f,,sizeof(f));
int res=,sum=;
for(int i=;i+K<=N;i++)
{
if((dir[i]+sum)%!=)
{
res++;
f[i]=;
}
sum+=f[i];
if(i-K+>=)
sum-=f[i-K+];
}
for(int i=N-K+;i<N;i++) //检查剩下的牛是否有面朝后方的情况
{
if((dir[i]+sum)%!=)
return -;
if(i-K+>=)
sum-=f[i-K+];
}
return res;
} void solve()
{
int K=;
int M=N;
for(int k=;k<=N;k++)
{
int m=calc(k);
if(m>= && M>m)
{
M=m;
K=k;
}
}
cout<<K<<" "<<M<<endl;
}
int main()
{
scanf("%d",&N);
int num=;
for(int i=;i<N;i++){
char ch;
cin>>ch;
if(ch=='B')
dir[num]=;
else
dir[num]=;
// cout<<dir[num]<<" ";
num++;
}
solve();
return ;
}

Face The Right Way POJ - 3276 (开关问题)的更多相关文章

  1. POJ 3276 (开关问题)

    题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...

  2. 反转(开关问题) POJ 3276

    POJ 3276 题意:n头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全部面朝前方.问:求最小操作m,再此基础上求k. 题解:1.5000头 ...

  3. POJ 3276 Face The Right Way 翻转(开关问题)

    题目:Click here 题意:n头牛排成一列,F表示牛面朝前方,B表示面朝后方,每次转向K头连续的牛的朝向,求让所有的牛都能面向前方需要的最少的操作次数M和对应的最小的K. 分析:一个区间反转偶数 ...

  4. POJ 1681 (开关问题+高斯消元法)

    题目链接: http://poj.org/problem?id=1681 题目大意:一堆格子,或白或黄.每次可以把一个改变一个格子颜色,其上下左右四个格子颜色也改变.问最后使格子全部变黄,最少需要改变 ...

  5. POJ 1222 (开关问题+高斯消元法)

    题目链接: http://poj.org/problem?id=1222 题目大意:一堆开关,或开或关.每个开关按下后,周围4个方向开关反转.问使最后所有开关都关闭的,开关按法.0表示不按,1表示按. ...

  6. poj 1830 开关问题

    开关问题 题意:给n(0 < n < 29)开关的初始和最终状态(01表示),以及开关之间的关联关系(关联关系是单向的输入a b表示a->b),问有几种方式得到最终的状态.否则输出字 ...

  7. POJ 1830 开关问题(高斯消元)题解

    思路:乍一看好像和线性代数没什么关系.我们用一个数组B表示第i个位置的灯变了没有,然后假设我用u[i] = 1表示动开关i,mp[i][j] = 1表示动了i之后j也会跟着动,那么第i个开关的最终状态 ...

  8. POJ 1830 开关问题(Gauss 消元)

    开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7726   Accepted: 3032 Description ...

  9. POJ 1830 开关问题 【01矩阵 高斯消元】

    任意门:http://poj.org/problem?id=1830 开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...

随机推荐

  1. ElasticSearch入门-搜索(java api)

    ElasticSearch入门-搜索(java api) package com.qlyd.searchhelper; import java.util.Map; import net.sf.json ...

  2. AngularJS(十):依赖注入

    本文也同步发表在我的公众号“我的天空” 依赖注入 依赖注入不是AngularJS独有的概念,而是现代软件开发与架构的范畴,但是在AngularJS中“依赖注入”是其核心思想之一,所以我们专门来学习一下 ...

  3. GreenDao 使用知识小y

    //关于 group by 的实现//--------------------XXXDao.queryBuilder().where(new WhereCondition.StringConditio ...

  4. 第1章 .Net应用程序体系结构

    1. CLR:公共语言运行库,是每种.Net编程语言都使用的运行库 Windows 8为Windows Store应用程序引入了一个新的编程接口:Windows运行库. C# 6 具有许多小而实用的语 ...

  5. 自己实现的简单的grid

    12年在第一家公司的时候,有过很长一段时间在前端的使用研究上.一开始的时候使用ExtJs4.0 MVC 来开发前端,觉得里面的风转的组件非常好用,Panel.window.tree等等,简化了对于前端 ...

  6. python 函数学习之sys.argv[1]

    一.sys 模块 sys是Python的一个「标准库」,也就是官方出的「模块」,是「System」的简写,封装了一些系统的信息和接口. 官方的文档参考:https://docs.python.org/ ...

  7. Sqlserver列出所有数据库名,表名,字段名【转】

    1.获取所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 注意: 表Master与SysDatabases之间有两个点 2.获取所 ...

  8. [Asp.Net] web api 部署注意事项

    在将web api项目部署到IIS上的时候 要将应用程序池设置成.net framework 4.0版本

  9. 自动化构建工具gulp的基础了解

    1.使用gulp的步骤 1.安装node检测是否安装好 cmd->node -v2.安装gulp 可以在npm或者在cnpm3.在node里面有个文件package.json.利用命令行npm ...

  10. JS每点击一次添加多少条数据

    很久不写文档,平时只写日记,所以对这个有点生疏,如果写的不好别介意. 今天闲的蛋疼,于是要写写白天的东西,并且以后也会一直更新(一直写)下去. 时间太仓促了,这几个月,今天算最晚的一次凌晨1点,吃不消 ...