【其它算法】Face The Right Way
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5278 | Accepted: 2462 |
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
Lines 2..N+1: Line i+1 contains a single character, F or B, indicating whether cow i is facing forward or backward.
Output
Sample Input
7
B
B
F
B
F
B
B
Sample Output
3 3
Hint
Source
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
#define LL long long
const int MAXN=10000;
const int INF=999999;
char last='F';
int N; int ansm=INF,ansk;
int f[5001],f2[5001];
int main(){
N=read();
char c;
for(int i=1;i<=N;i++){
cin>>c;
if(c!=last) f[i]=1;
last=c;
}
for(int k=1;k<=N;k++){
int cnt=0;
for(int i=1;i<=N;i++) f2[i]=f[i];
for(int i=1;i<=N-k+1;i++)
if(f2[i]){f2[i+k]^=1;cnt++;}
for(int i=N-k+2;i<=N;i++)
if(f2[i]) {cnt=INF;break;}
if(cnt==INF) continue;
if(cnt<ansm){
ansm=cnt;
ansk=k;
}
}
printf("%d %d\n",ansk,ansm);
}
最新文章
- mybaties # , $
- 单页Web应用:
- iOS - Xcode 常用快捷键
- uva 10692 Huge Mods 超大数取模
- lintcode:快乐数
- c++实现输入法窗口自定义的代码
- 如何让Vim显示dos下的^M符号
- InstallShield:自己备份
- VS2012 快捷键 VS Resharper 设置
- 常用的一些js和css
- C++11 中值得关注的几大变化(网摘)
- 【Nginx系列】Nginx虚拟主机的配置核日志管理
- 在web应用中使用Log4j 2
- 优先使用TimeUnit类中的sleep()
- C#嵌入子窗体,判断子窗体是否打开了
- docker镜像加速器
- JobService相关
- 如何利用FPGA进行时序分析设计
- CentOS7 安装redis 并且设置成服务自动启动
- JavaScript 里面的整数 位 操作