题目概括

题目描述

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. dozer工具类

    jar:commons-beanutils-1.9.3.jar.commons-lang-2.6.jar.dozer-5.3.2.jar.jcl-over-slf4j-1.7.25.jar.slf4j ...

  2. linux lnmp下无法使用mail发邮件的两种解决方法

    在配置了lnmp环境后,出现了mail函数不能发送邮件的问题,其实有两种方法,一是使用sendmail组件,而是使用postfix. 方法一,使用sendmail组件来发邮件  1.安装 sendma ...

  3. java基础系列(一):Number,Character和String类及操作

    这篇文章总结了Java中最基础的类以及常用的方法,主要有:Number,Character,String. 1.Number类 在实际开发的过程中,常常会用到需要使用对象而不是内置的数据类型的情形.所 ...

  4. Redis List集合 使用

    列表类型(list)用于存储一个有序的字符串列表,常用的操作是向队列两端添加元素或者获得列表的某一片段.列表内部使用的是双向链表(double linked list)实现的,所以向列表两端添加元素的 ...

  5. 多线程调用有参数的方法---c# Thread 与 Task

    C#实现多线程的方式:Task——任务   简介 .NET 4包含新名称空间System.Threading.Tasks,它 包含的类抽象出了线程功能. 在后台使用ThreadPool. 任务表示应完 ...

  6. Codis-proxy的配置和启动

    生成配置文件,即将现有的配置文件输出到指定目录位置: ./codis-proxy --default-config | tee conf/proxy.toml 修改配置文件信息: vi conf/pr ...

  7. yml 文件中使用环境变量

    Spring Boot 中可以用 spring.profiles.active 参数来指定系统环境,让系统加载不同的配置文件. 可以在程序启动的时候加上参数来指定需要的配置 java -Dspring ...

  8. nginx 四个主要组成部分

    1.Nginx 二进制可执行文件 由各模块源码编译出的一个文件 2.Nginx.conf 配置文件 控制nginx如何运行 3.access.log 访问日志 记录http请求信息 4.error.l ...

  9. CDH建表字符集问题

    登陆metadata数据库: show variables like 'character_set%'; alter database hivemeta default character set l ...

  10. 1、6 登陆 通过name 查到一个对象,如果没有。提示用户名不存在,再将查到的pwd 加传过来的pwd(需要加密)比较

    @RequestMapping("/login") public Object login(Student student) { Student s=studentService. ...