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 ;
}
Face The Right Way POJ - 3276(区间)的更多相关文章
- 反转(开关问题) POJ 3276
POJ 3276 题意:n头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全部面朝前方.问:求最小操作m,再此基础上求k. 题解:1.5000头 ...
- POJ 3276 (开关问题)
题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...
- POJ 2955 (区间DP)
题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...
- POJ 1651 (区间DP)
题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少 ...
- POJ 3468 区间更新,区间求和(经典)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 72265 ...
- POJ 3264 区间最大最小值Sparse_Table算法
题目链接:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total ...
- poj 3276(反转)
传送门:Problem 3276 参考资料: [1]:挑战程序设计竞赛 先献上AC代码,题解晚上再补 题意: John有N头牛,这些牛有的头朝前("F"),有的朝后("B ...
- poj 3485 区间选点
题目链接:http://poj.org/problem?id=3485 题意:X轴上公路从0到L,X轴上下有一些点给出坐标代表村庄,问在公路上最少建几个出口才能使每个村庄到出口的距离不超过D. 以村庄 ...
- POJ 2104 区间第k大(主席树)
题目链接:http://poj.org/problem?id=2104 题目大意:给定还有n个数的序列,m个操作,每个操作含有l,r,k,求区间[l,r]第k大 解题思路:线段树只能维护序列的最大值最 ...
随机推荐
- Mac笔记本使用小道解答集
如何设置Mac默认应用程序 https://www.jianshu.com/p/0f912e6c846c 苹果本安装微软雅黑 下载微软雅黑字体Mac版 解压.ttf 拖拽放入 我的电脑/资源库/fon ...
- JavaScript实现集合与字典
JavaScript实现集合与字典 一.集合结构 1.1.简介 集合比较常见的实现方式是哈希表,这里使用JavaScript的Object类进行封装. 集合通常是由一组无序的.不能重复的元素构成. 数 ...
- 手写Promise原理
我的promise能实现什么? 1:解决回调地狱,实现异步 2:可以链式调用,可以嵌套调用 3:有等待态到成功态的方法,有等待态到失败态的方法 4:可以衍生出周边的方法,如Promise.resolv ...
- C++ 随笔练习 求和
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { int ...
- Spring解决循环依赖,你真的懂了吗?
导读 前几天发表的文章SpringBoot多数据源动态切换和SpringBoot整合多数据源的巨坑中,提到了一个坑就是动态数据源添加@Primary接口就会造成循环依赖异常,如下图: 这个就是典型的构 ...
- Git&sourceTree软件安装、使用说明及遇到问题解决
一.软件版本 1.Git版本为1.9.5 2.Source版本为1.5.2 二.软件安装步骤 1.Git安装步骤 1)双击Git安装文件进入下图界面,单击Next 2)继续Next 3)进入Selec ...
- 【简说Python WEB】视图函数操作数据库
目录 [简说Python WEB]视图函数操作数据库 系统环境:Ubuntu 18.04.1 LTS Python使用的是虚拟环境:virutalenv Python的版本:Python 3.6.9 ...
- CodeMixer工具,完美替代ChaosTool,iOS添加垃圾代码工具,代码混淆工具,代码生成器,史上最好用的垃圾代码添加工具,自己开发的小工具
新工具 ProjectTool 已上线 这是一款快速写白包工具,秒级别写H5游戏壳包,可视化操作,极易使用,支持Swift.Objecive-C双语言 扣扣交流群:811715780 进入 Proje ...
- P1361 小M的作物 【网络流】【最小割】
题目描述 小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物的种子,每种作物的种子有1个(就是可以种一棵作物)(用1...n编号). 现在,第i种作物种植在A中种植可 ...
- JS 冒泡排序详解
冒泡排序原理:比较相邻两个数的大小,如果第一个数大于第二个数,那么交换位置,从第一位数开始,对后面每一对相邻的数据进行同样的比较和交换,直到最后没有任何一位需要进行比较大小和交换: 思路演算: arr ...