Face The Right Way POJ - 3276(区间)
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
Lines 2..
N+1: Line
i+1 contains a single character,
F or
B, indicating whether cow
i is facing forward or backward.
Output
K and
M
Sample Input
7
B
B
F
B
F
B
B
Sample Output
3 3
Hint
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=5e3+;
int n;
char fac;
bool face[maxn],f[maxn];//face: 0->qian 1->hou f:0->don't need turn ,1->need
int Fan(int len){
memset(f,,sizeof(f));
int cishu=,sum=;//sum->已经turn的次数 cishu->turn的总次数
for(int i=;i+len-<=n;++i){
if((face[i]+sum)%==){//i朝后
cishu++;
f[i]=;
}
sum+=f[i];
if(i-len+>=)sum-=f[i-len+];//现在 sum 是下一个i已经turn的次数了
}
//因为最后一个i是离最后一头牛len-1长度,检查未处理过的牛是否朝后,if this,无解
for(int i=n-len++;i<=n;++i){
if((face[i]+sum)%==)return -;
if(i-len+>=)sum-=f[i-len+];
}
return cishu;
}
void Solve(){
int K=n,cishu=n;
for(int len=;len<=n;++len){//枚举区间长度
int m=Fan(len);
if(m>=&&cishu>m){
cishu=m;K=len;
}
}
printf("%d %d\n",K,cishu);
return;
}
int main(){
// freopen("1.in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;++i){
scanf(" %c",&fac);
if(fac=='B')face[i]=;
}
Solve();
return ;
}
最新文章
- Android Json处理框架
- PHP require class
- Bootstrap 固定定位(Affix)
- 51nod1495 中国好区间
- ASP大数据量使用GetRows()提升速度
- Effective c++ 第一章 让自己习惯C++
- Cells(Rows.Count, 1).End(xlUp).Row的含义
- JavaScript JSON timer(计时器) AJAX HTTP请求 同源策略 跨域请求
- uml的图与代码的转换——类图
- Markdown语法及html内嵌
- python kline
- 第七届蓝桥杯大赛个人赛决赛(软件类C语言B组)第一题:一步之遥
- webapack
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
- fastcgi协议分析与实例
- deno学习三 官方提供的方便deno 安装方式
- Math.random控制随机数范围
- 2013长春网赛1010 hdu 4768	Flyer
- 20145312《网络对抗》MSF基础
- javascript 大中小括号的区别