Face The Right Way POJ - 3276 (开关问题)
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
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
#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 ;
}
最新文章
- LNMP平台搭建---MySQL安装篇
- socket服务器开发中的SO_REUSEADDR选项与让人心烦的TIME_WAIT
- Selenium Waits
- 股票k线
- 指定IE浏览器渲染方式
- BZOJ 2762: [JLOI2011]不等式组( 平衡树 )
- kubernetes 留言版DEMO
- List<;T>;转换为ObservableCollection<;T>;
- [Angular Tutorial] 3-Components
- Jungle Roads(最小生成树)
- IntelliJ IDEA(六) :Settings(下)
- sau交流学习社区第三方登陆github--oauth来实现用户登录
- 给iPhone手机安装*.ipa
- kafka_2.11-2.0.0_常用操作
- 搭建Fabric网络(三)artifacts是怎么生成的:cryptogen和configtxgen
- OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 1.操作系统环境配置
- Python 练习:三级菜单选择城市(二)
- $\mathfrak {reputation}$
- 【Spark】Spark-架构
- Table转换成实体、Table转换成实体集合(可转换成对象和值类型)
热门文章
- mysql5.5.28.tar.gz编译安装操作笔记
- Oracle单列函数
- C#字符串自增自减算法
- [Java][Liferay] 解决Liferay ext项目deploy的问题
- 适用于 Internet Explorer 11 的企业模式
- mathtype 章节号 Equation Chapter 1 Section 1 的去除
- office密匙
- java Vamei快速教程05 实施接口
- 【BZOJ3940】[USACO2015 Feb] Censoring (AC自动机的小应用)
- fmt - 简易的文本格式优化工具 simple optimal text formatter