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. 让zepto支持requirejs的方法

    window.Zepto = Zepto '$' in window || (window.$ = Zepto) if ( typeof define === "function" ...

  2. 在Magento System Configuration页面添加配置项

    以 Jp_Coupon 模块为例: 目标: 在 System configuration 页面添加一个 JP tab, 在JP中添加 Coupon section, 然后给 Coupon sectio ...

  3. $(formName).data(“bootstrapValidator”).getFieldElements('fieldName'); 校验单个字段

    问题也出自于业务系统后台,应该来说也比较常见吧 房产类型分为一抵和二抵,二抵的时候用户必须填写一抵债权金额,一抵的时候则不显示一抵债权金额也不校验,因为我所有的校验都是写在标签上,哪些必填直接写在标签 ...

  4. Spring 的AOP

    AOP:面向切面编程,相对于OOP面向对象的编程 Spring的AOP的存在的目的是为了解耦.AOP可以让一组类共享相同的行为.在OOP中只能通过继承类和实现接口,来使代码的耦合度增强,且类继承只能为 ...

  5. 彻底解决Android 应用方法数不能超过65K的问题

    作为一名Android开发者,相信你对Android方法数不能超过65K的限制应该有所耳闻,随着应用程序功能不断的丰富,总有一天你会遇到一个异常: Conversion to Dalvik forma ...

  6. 根据accept-language自动设置UICulture和Culture

    在web.config中添加如下配置: <system.web> <globalization uiCulture="auto" culture="au ...

  7. jQuery-名称符号$与其他库函数冲突

    1.通过全名替代简写的方式来使用 jQuery jQuery("button").click(function(){ jQuery("p").text(&quo ...

  8. HDU2612 BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 , 一道比较简单的广搜(BFS)题目. 算法: 设置两个dist[][]数组,记录Y和M到几个K ...

  9. UVA 11990 ``Dynamic'' Inversion (序列分治)

    26天以前做过的一道题,之前的做法是分治预处理,树套树在线修改,复杂度为O(nlogn+m*logn*logn),代码量较大. 本来想学习一下cdq分治的,看到论文上的凸包.斜率就暂时放一边了,只知道 ...

  10. POJ 2229 Sumsets(递推,找规律)

    构造,递推,因为划分是合并的逆过程,考虑怎么合并. 先把N展开成全部为N个1然后合并,因为和顺序无关,所以只和出现次数有关情况有点多并且为了避免重复,分类,C[i]表示序列中最大的数为2^i时的方案数 ...