Face the right way(反转问题,思维题)
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
Line 1: A single integer: N
Lines 2… N+1: Line i+1 contains a single character, F or B, indicating whether cow i is facing forward or backward.
Output
Line 1: Two space-separated integers: K and M
Example input
7
B
B
F
B
F
B
B
Example output
3 3
译成人话:
有n头奶牛排成一排,有的朝前有的朝后,现在你可以使k(每次翻转必须是k头)头奶牛一次性翻转朝向(n>=k>=1),问你最少的翻转次数和此时对应的k值。
题目要求我们找到最优的k值,也就是区间长度,我们可以从小到大枚举这个k,这稍微有点区间DP的味道,但并不是一码事,对于反转来讲,如果反转的次数为奇数,那么这个区间的牛算是被扭了,但如果次数为偶数,就相当于没动,我们可以依此对1~n-len+1(len就是k)进行遍历,我们每枚举一个点,这个点就可以看成一个len区间的最左端,如果这个牛是反的,我们就把后面的区间都扭一下
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=5000+10;
int n,f[maxn];//若f[i]为1,则说明从i到i+len-1都被扭了
char cow[maxn];
int solve(int len){
memset(f,0,sizeof(f));
int ans=0;//记录反转的次数
int sum=0;//记录区间中牛被反转的次数
for(int i=1;i<=n-len+1;i++){
if(sum%2==0&&cow[i]=='B')//因为反转次数是偶数,所以牛都相当于没变,为了把i扭过来,次数要加1
f[i]=1,ans++;
else if(sum%2==1&&cow[i]=='F')//因为反转次数是奇数,而且牛i朝前
f[i]=1,ans++;
sum+=f[i];
if(i-len+1>=1) sum-=f[i-len+1];//当一个len区间被扭完,为了避免后面不该标记的被标记,把前面的减去
}
for(int i=n-len+2;i<=n;i++){
if(sum%2==0&&cow[i]=='B') return -1;//不满足则返回-1
else if(sum%2==1&&cow[i]=='F') return -1;
sum+=f[i];
if(i-len+1>=1) sum-=f[i-len+1];
}
return ans;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)
scanf(" %c",&cow[i]);
int k=0x3f3f3f3f,minn=0x3f3f3f3f;
for(int i=1;i<=n;i++){
int p=solve(i);
if(p>=0&&p<minn){
minn=p,k=i;
}
}
cout<<k<<" "<<minn<<endl;
return 0;
}
Face the right way(反转问题,思维题)的更多相关文章
- UVA.679 Dropping Balls (二叉树 思维题)
UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...
- cf796c 树形,思维题
一开始以为是个树形dp,特地去学了..结果是个思维题 /* 树结构,设最大点权值为Max,则答案必在在区间[Max,Max+2] 证明ans <= Max+2 任取一个点作为根节点,那么去掉这个 ...
随机推荐
- Volcano火山:容器与批量计算的碰撞
[摘要] Volcano是基于Kubernetes构建的一个通用批量计算系统,它弥补了Kubernetes在“高性能应用”方面的不足,支持TensorFlow.Spark.MindSpore等多个领域 ...
- 性能测试中TPS上不去的原因
TPS(Transaction Per Second):每秒事务数,指服务器在单位时间内(秒)可以处理的事务数量,一般以request/second为单位. 压测中为什么TPS上不去的原因: .网络带 ...
- iOS -实现imageView中的button响应点击事件的方法
<pre name="code" class="cpp" style="font-size: 13px;">/** imagev ...
- CSS学习:overflow:hidden解决溢出,坍塌,清除浮动
overflow:hidden是overflow属性的一个神奇用法,它可以帮助我们隐藏溢出的元素,清除浮动和解除坍塌. CSS样式: .container{ background-color: bla ...
- 跨域问题 - Nginx反向代理
Nginx反向代理的思路,就是通过Nginx解析URL地址的时候进行判断,将请求转发的具体的服务器上. 解决思路 跨域问题,是由于JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象. ...
- (转)Zookeeper全解析——Paxos作为灵魂
原计划在介绍完ZK Client之后就着手ZK Server的介绍,但是发现ZK Server所包含的内容实在太多,并不是简简单单一篇Blog就能搞定的.于是决定从基础搞起比较好. 那么ZK Serv ...
- Linux中GitLab的部署
1.版本控制介绍 版本控制最主要的功能就是追踪文件的变更.它将什么时候.什么人更改了文件的什么内容等信息忠实地了记录下来.每一次文件的改变,文件的版本号都将增加.除了记录版本变更外,版本控制的另一 ...
- Razor 视图
Razor 视图 关于视图引擎 视图引擎简单理解就是能够支持对视图的解析,在 ASP.NET MVC 中,视图引擎的作用就是把视图处理成浏览器能够执行的 HTML 代码,不同的视图引擎,语法规则不一样 ...
- Spark原始码系列(五)分布式缓存
问题导读:spark缓存是如何实现的?BlockManager与BlockManagerMaster的关系是什么? 这个persist方法是在RDD里面的,所以我们直接打开RDD这个类. def pe ...
- 通过el-tree 实现每次可选中一个节点方案(非checkbox)
<el-tree v-if="orgDrawer" :data="orgTree" size="medium" ref="o ...