题目概括

题目描述

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. C语言 班级档案管理系统实现

    代码地址:github地址 班级档案管理系统 原题目要求是对一个有N个学生的班级,通过该系统实现对该班级学生的基本信息进行录入. 显示.修改.删除.保存等操作的管理. 由于个人需要,我单独将项目改造为 ...

  2. 不使用局部变量和for循环或其它循环打印出如m=19,n=2結果为2 4 8 16 16 8 4 2形式的串

    需求:不使用局部变量和for循环或其它循环打印形如:2 4 8 16 16 8 4 2 这样的串 代码MainTest.java package com.szp.study.javase.specia ...

  3. 2017年度好视频,吴恩达、李飞飞、Hinton、OpenAI、NIPS、CVPR、CS231n全都在

    我们经常被问:机器翻译迭代了好几轮,专业翻译的饭碗都端不稳了,字幕组到底还能做什么? 对于这个问题,我们自己感受最深,却又来不及解释,就已经边感受边做地冲出去了很远,摸爬滚打了一整年. 其实,现在看来 ...

  4. [转帖]降低 80% 的读写响应延迟!我们测评了 etcd 3.4 新特性(内含读写发展史)

      降低 80% 的读写响应延迟!我们测评了 etcd 3.4 新特性(内含读写发展史) https://www.cnblogs.com/alisystemsoftware/p/11555426.ht ...

  5. javascript策略模式的应用!

    最近在看<JavaScript设计模式与开发实践>这本书,受益匪浅,小记录一下书中的各个demo,加深理解: 策略模式的定义是:定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替 ...

  6. SC创建服务编写bat脚本

    新建bat脚本,并写入一下文本保存 sc create "DevFast.SupportGPSWarmService" binpath= "%cd%\DevFast.Su ...

  7. websocket抓包

    https://www.cnblogs.com/xiaoniuzai/p/7588739.html http://blog.sina.com.cn/s/blog_12df1b9e60102vyeq.h ...

  8. Vue中常用知识点demo

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  9. Go语言学习笔记(7)——函数和方法

    Go语言中同时有函数和方法! 函数: go程序必须要包含一个main函数.main函数不能有任何参数和返回值! 1. 定义方法示例: func max(num1, num2 int) int { // ...

  10. 剑指offer(9)——用两个栈实现队列

    题目: 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 思路: 首先定义两个栈stack1. ...