[USACO07MAR]面对正确的方式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.
\(N\)头牛排成一列。每头牛或者向前或者向后。为了让所有牛都 面向前方,农夫每次可以将\(K\)头连续的牛转向,求操作的最少次数\(M\)和对应的最小\(K\)。
\(B\)表示当前奶牛往后看,\(F\)表示奶牛往前看。
样例输入输出格式
输入格式
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.
输出格式
Line 1: Two space-separated integers: K and M
输入输出样例
输入 #1
7
B
B
F
B
F
B
B
输出 #1
3 3
数据范围
样例解释
For K = 3, the machine must be operated three times: turn cows (1,2,3), (3,4,5), and finally (5,6,7)
1 \le K \le N \\\\
\]
解题报告
题意理解
\(N\)头牛排成一列。每头牛或者向前或者向后。为了让所有牛都 面向前方,农夫每次可以将\(K\)头连续的牛转向,求操作的最少次数\(M\)和对应的最小\(K\)。
算法解析
这道题目,其实就是一个贪心的题目。
我们知道,因为同一个点翻转两次相当于没有翻转,这就是异或的特性
每个点,只有正反之分,所以这就是01
然后设计贪心,如下所示:
从左到右对于出现的每一个\(0\),然后我们就不得不翻转一次,从当前点开始的区间。
但是,我们发现这个贪心,时间复杂度过大
从左到右枚举。\(O(n)\)
枚举区间长度。\(O(n)\)
区间翻转。\(O(n)\)
综上所述,复杂度为\(O(n^3)\)
我们怎么降维打击时间复杂度呢,我们思考一下。
区间翻转,然后又是类似于区间异或。
我们似乎可以使用,差分,这个支持区间异或的算法。
于是,我们可以通过,差分优化本题目。
代码解析
#include <bits/stdc++.h>
using namespace std;
const int N=5010;
int n,a[N],sum[N],d[N],cnt,ans=1e9,pos,now,ok;
char c;
inline void init()
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
getchar();
c=getchar();
c=='B' ? a[i]=0:a[i]=1;//设置
}
}
inline void work()
{
for(int k=1; k<=n; k++)
{
now=0,cnt=0,ok=1;
memset(d,0,sizeof(d));
for(int i=1; i<=n; i++)
{
now^=d[i];//异或
if(!a[i]^now)//是时候要异或了,发现了反方向的奶牛
{
if(i+k-1>n)//不能超过最大长度
{
ok=false;
break;
}
now^=1;//异或
d[i+k]^=1;//同时在区间右端点异或,保证最后没有问题
cnt++;
}
}
if(ok && ans>cnt)//记得更新答案哦
ans=cnt,pos=k;
}
printf("%d %d\n",pos,ans);
}
int main()
{
init();
work();
return 0;
}
[USACO07MAR]面对正确的方式Face The Right Way的更多相关文章
- bzoj1704 / P2882 [USACO07MAR]面对正确的方式Face The Right Way
P2882 [USACO07MAR]面对正确的方式Face The Right Way $n<=5000$?枚举翻转长度,顺序模拟就ok了 对于每次翻转,我们可以利用差分的思想,再搞搞前缀和. ...
- 洛谷P2882 [USACO07MAR]面对正确的方式Face The Right Way(贪心)
题目描述 Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing forwar ...
- 以正确的方式开源 Python 项目
以正确的方式开源 Python 项目 大多数Python开发者至少都写过一个像工具.脚本.库或框架等对其他人也有用的工具.我写这篇文章的目的是让现有Python代码的开源过程尽可能清 晰和无痛.我不是 ...
- iOS开发小技巧--相机相册的正确打开方式
iOS相机相册的正确打开方式- UIImagePickerController 通过指定sourceType来实现打开相册还是相机 UIImagePickerControllerSourceTypeP ...
- const、static和extern的正确使用方式
我们在看一些大牛的第三方时,里面会出现很多const.static和extern,尤其是const和static,const和extern的结合使用,直接令很多小伙伴懵逼了,今天就详细讲解一下这三个关 ...
- @synthesize的正确使用方式
@synthesize的正确使用方式 一. @synthesize的错误使用方式 类1和类2是继承关系, name是类1的属性 但是类2的实现里加入了@synthesize name = _name; ...
- Xcode 的正确打开方式——Debugging(转载)
Xcode 的正确打开方式——Debugging 程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能 ...
- 以正确的方式开源 Python 项目 - 技术翻译 - 开源中国社区
以正确的方式开源 Python 项目 - 技术翻译 - 开源中国社区 以正确的方式开源 Python 项目 英文原文:Open Sourcing a Python Project the Right ...
- 以正确的方式开源 Python 项目(转)
大多数Python开发者至少都写过一个像工具.脚本.库或框架等对其他人也有用的工具.我写这篇文章的目的是让现有Python代码的开源过程尽可能清晰和无痛.我不是简单的指——“创建一个GitHub库,提 ...
随机推荐
- Efcore迁移
Efcore迁移 Add-Migration XX:1.根据模型的实际结构对比当前快照,从而生成新迁移文件的Up和Down方法2.根据模型的实际结构修改快照和新迁移文件---------------- ...
- react做的简单的购物车
###第一步 :首先电脑上已经安装react的脚手架 cnpm install create-react-app -g ###第二步 :创建项目 creact-react-app 项目 ...
- laydate时间控件:开始时间,结束时间最大最小值
时间控件地址及插件下载链接:https://www.layui.com/doc/modules/laydate.html 填充时间已两个功能为例: 1.添加功能 :时间 规则:选择开始时间后,点击结束 ...
- pureftp 超时 mlsd
问题起因,新项目搭建系统环境,ftp总有问题 能连接成功,但总是时好时不好,解决处理,忘有用 mlsd 超时列目录问题 (一会好一会不好) # a.指定被动端口中,如20000-60000,在ipta ...
- Python函数或者类的时间参数的默认值设为datetime.date.today()引起的问题
定义了函数def main(start_date=datetime.date.today(), end_date=datetime.date.today()): pass 函数在项目启动后, end_ ...
- Vue 开发技巧或者说Vue知识点梳理(转,自个学习)
Vue 组件通讯 ——常见使用场景可以分为三类: 父子通信: 父向子传递数据是通过 props,子向父是通过 events($emit):通过父链 / 子链也可以通信($parent / $child ...
- python 模块import(26)
一.模块简介 python开发中,每一个.py文件都可以看作是一个模块,模块内部的函数或者方法可以被其他模块调用,至于函数或者方法是具体如何实现的,调用者不需要关心. 假如项目中既含有UI也有逻辑代码 ...
- MongoDB添加删除节点
副本集添加删除节点 sharding添加删除节点 先将节点设置为hidden,再remove
- 【数据结构】P1981 表达式求值
题目描述 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. 输入格式 一行,为需要你计算的表达式,表达式中只包含数字.加法运算符“++”和乘法运算符“×”,且没有括号,所有参与运算的数字 ...
- localStorage存取储数组。(存对象同理)
var array = ['111','222','333','444','555']; 存window.localStorage.setItem('array',JSON.stringify(arr ...