题目概括

题目描述

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 N \le 5000 \\\\
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的更多相关文章

  1. bzoj1704 / P2882 [USACO07MAR]面对正确的方式Face The Right Way

    P2882 [USACO07MAR]面对正确的方式Face The Right Way $n<=5000$?枚举翻转长度,顺序模拟就ok了 对于每次翻转,我们可以利用差分的思想,再搞搞前缀和. ...

  2. 洛谷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 ...

  3. 以正确的方式开源 Python 项目

    以正确的方式开源 Python 项目 大多数Python开发者至少都写过一个像工具.脚本.库或框架等对其他人也有用的工具.我写这篇文章的目的是让现有Python代码的开源过程尽可能清 晰和无痛.我不是 ...

  4. iOS开发小技巧--相机相册的正确打开方式

    iOS相机相册的正确打开方式- UIImagePickerController 通过指定sourceType来实现打开相册还是相机 UIImagePickerControllerSourceTypeP ...

  5. const、static和extern的正确使用方式

    我们在看一些大牛的第三方时,里面会出现很多const.static和extern,尤其是const和static,const和extern的结合使用,直接令很多小伙伴懵逼了,今天就详细讲解一下这三个关 ...

  6. @synthesize的正确使用方式

    @synthesize的正确使用方式 一. @synthesize的错误使用方式 类1和类2是继承关系, name是类1的属性 但是类2的实现里加入了@synthesize name = _name; ...

  7. Xcode 的正确打开方式——Debugging(转载)

    Xcode 的正确打开方式——Debugging   程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能 ...

  8. 以正确的方式开源 Python 项目 - 技术翻译 - 开源中国社区

    以正确的方式开源 Python 项目 - 技术翻译 - 开源中国社区 以正确的方式开源 Python 项目 英文原文:Open Sourcing a Python Project the Right ...

  9. 以正确的方式开源 Python 项目(转)

    大多数Python开发者至少都写过一个像工具.脚本.库或框架等对其他人也有用的工具.我写这篇文章的目的是让现有Python代码的开源过程尽可能清晰和无痛.我不是简单的指——“创建一个GitHub库,提 ...

随机推荐

  1. python 元组tuple - python基础入门(14)

    在上一篇文章中我们讲解了关于python列表List的相关内容,今天给大家解释一下列表List的兄弟 – 元组,俗称: tuple. 元组tuple和列表List类似,元组有如下特点: 1.由一个或者 ...

  2. Hbuider APP打包流程

      1,下载HBuilder,注册并登陆.首先打开“文件”-“新建”-“移动APP”,输入“应用名称”,“位置”可以根据需要自己选择即可,“选择模板”建议选择空模板: 2,新建完成后, 在项目管理器会 ...

  3. [转帖]CentOS 8 都发布了,你还不会用 nftables?

    CentOS 8 都发布了,你还不会用 nftables? https://www.cnblogs.com/ryanyangcs/p/11611730.html 改天学习一下 原文链接:CentOS ...

  4. SecureCRT 配色方案

    整体效果: 一:Options -- Global Options 1.Normal colors     2.Bold colors 二 :Options -- Session Options 1. ...

  5. 【Docker】:docker安装ELK(logstash,elasticsearch,kibana)

    一:安装logstash 1.拉取镜像 docker pull logstash:5.6.11 2.创建目录 mkdir /docker/logstash cd /docker/logstash 3. ...

  6. SAS学习笔记23 线性回归、多元回归

    线性回归 由样本资料计算的回归系数b和其他统计量一样,存在抽样误差,因此,需要对线性回归方程进行假设检验 1.方差分析 2.t检验 相关系数的假设检验 相关系数(correlation coeffic ...

  7. Kafka无法消费?!我的分布式消息服务Kafka却稳如泰山!

    在一个月黑风高的夜晚,突然收到现网生产环境Kafka消息积压的告警,梦中惊醒啊,马上起来排查日志. 问题现象:消费请求卡死在查找Coordinator Coordinator为何物?Coordinat ...

  8. Http中的同步请求和异步请求

    最近在上springmvc的JSON数据交换的时候,老师下课提了一个课后问题:什么是异步请求?什么是同步请求?我想大部分同学听到这个问题的时候应该和我一样不知所云.现在,给大家分享一篇关于同步请求和异 ...

  9. 如何将Linux的工程原封不动地移植到Windows上面

    习惯在Linux下进行开发.但是由于工作需要,不得不与其他使用Windows的项目组同事对接,同事要求我给出可用的程序,而我只有基于makefile的传统工程. 改动到VS工程上发现一部分头文件在Wi ...

  10. C#对象转换工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...