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大 解题思路:线段树只能维护序列的最大值最 ...
随机推荐
- 聊聊.netcore采坑那一些事之系统时间and文件路径
聊聊.netcore采坑那一些事之系统时间and文件路径 Hi,小伙伴大家好,最近工作比较忙,很久没有和大家分享点东西了.这个周末都加了两天班.公司的新项目都是采用.netcore来开发,在开发过程中 ...
- springboot1.5.9整合websocket实现实时显示的小demo
最近由于项目需要实时显示数据库更新的数据变化情况,一开始想过在前端使用ajax异步轮询方法实现,但后面考虑到性能和流量等要求,就放弃该方法而选择使用websocket(毕竟现在springboot整合 ...
- NTP网络时钟服务器品牌
NTP网络时钟服务器品牌 在科技的不断进步和发展下,时钟的种类和功能也在发生着变化,以满足人们的各种需求,时钟从原始的机械时钟发展成具有多钟功能的时钟.而时钟服务器主要是给时钟提供时间信息的,时钟服务 ...
- MySQL数据库常用命令行整理(表格)
Laplace Kang 2020-03-13T08:33:09Z 2020-03-14T17:35:53Z Sheet1 12480 9 600 600 6 9600 23040 0 0 600 0 ...
- http详解和分析
1.http是什么? http 是一种超文本传输协议原名是这个Hypertext Transfer Protocol -- HTTP/1.1 可以百度查看http的RFC文档编号为RFC-2616 连 ...
- JavaScript初学者
学习如逆水行舟,不进则退.要逆流而上,逆战! 学习JavaScript这门语言,作为一个初学者,最重要的就是扎实的基础. 只要有了扎实的功底,在后期的学习中才能来去自如的应对各种逻辑难题 下面我们就来 ...
- JAVA-迭代器\增强型for循环。(新手)
//导入的包.import java.lang.reflect.Array;import java.util.*;//创建的一个类.public class zylx1 { //公共静态的主方法. p ...
- oracle中plsql练习题----查询姓为“SMITH”的员工信息,并输出其员工号、姓名、工资、部门号。 – –如果该员工不存在,则插入一条新记录,员工号为2012,员工姓名为“Smith”,工资为7500元,入职日期为“2002年3月5日”,部门号为50。 – –如果存在多个名“Smith”的员工,则输出所有名为“Smith”的员工号、姓名、工资、入职日期、部门号L。
一.思路:首先判断这个查询的是emp表,需要接收值,声明一个rowtype类型接收数据即可,第二是,存在exception,需要处理,exception中有两种异常,分别处理即可,分别输出即可. 二. ...
- Kafka的面试题
1.Kafka的设计是什么样的呢?Kafka将消息以topic为单位进行归纳将向Kafka topic发布消息的程序成为producers.将预订topics并消费消息的程序成为consumer.Ka ...
- 普通人学习rust——从零到放弃 变量、不可变量、常量
普通人学习rust--从零到放弃 变量.不可变量.常量 环境 本文章内容基于如下环境,如若出入请参考当前环境. rustc 1.42.0 (b8cedc004 2020-03-09) cargo 1. ...