题目描述

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头牛排成一列1<=N<=5000。每头牛或者向前或者向后。为了让所有牛都 面向前方,农夫每次可以将K头连续的牛转向1<=K<=N,求操作的最少 次数M和对应的最小K。
输入格式

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)

枚举K,用差分修改一段区间,时间复杂度为n^2。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;const int maxn = 1e4 + 10;
int a[maxn];
int vis[maxn];
int n;int main() {scanf("%d", &n);getchar();char c;for(int i = 1; i <= n; i++) {scanf("%c", &c);getchar();a[i] = c == 'F';}int step = 1e9, len = 0;for(int k = 1 ; k <= n; k++) {memset(vis, 0, sizeof(vis));int tem = 0, f = 0, tag = 0;for(int i = 1; i <= n; i++) {if(vis[i]) tag--;if((a[i] + tag) % 2 == 0) {if(i + k - 1 <= n) {tag++, vis[i + k] = true;tem++;}else f = true;}//   cout << tag << " ";}
//      cout << "   " << k << endl;if(f == 0 && tem < step)    step = tem, len = k;}printf("%d %d", len, step);return 0;
}

P2882 [USACO07MAR]Face The Right Way G 【贪心 + 差分】相关推荐

  1. 贪心+差分的题目:gaming

    原题链接 题目描述 "漆黑的夜晚"是一位游戏大神.他玩的游戏共有 n 个挑战房间,和 m 个 debuff.他非常强,只要不是带着所有的 debuff,他都能打过 boss 获得胜 ...

  2. P1090 合并果子 / [USACO06NOV] Fence Repair G(贪心+优先队列) 洛谷

    题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...

  3. 中石油训练赛 - 围栏翻新(思维+贪心+差分)

    题目描述 小明的破旧围栏又要喷涂油漆了.围栏由N个木板构成,每个宽度都为1cm,但是高度各不相同.他给自己买了一个喷漆机器,喷涂头恰好也是1cm宽. 小明的喷漆机器是直接喷射的,因此喷头的每一个部位必 ...

  4. jzoj5698-[gdoi2018day1]密码锁【贪心,差分】

    正题 题目大意 nnn个数字,每次可以让一个区间加或减111.然后数字是一个[0,m−1][0,m-1][0,m−1]的循环,求最少次数让所有数字变成000 解题思路 我们做一个%m\%m%m意义下的 ...

  5. 阿里春招笔试2020.3.27(贪心/差分/概率)

    申明:大概题意是从牛客网讨论区嫖的,题目的输入.输出以及数据数据范围也有些不知,大家看看思路就好,这些细节就不管了QAQ.有错欢迎纠正~ 3.27 题目一(贪心) 给定字符串s1,s2,求从s1变为s ...

  6. Too Many Segments (easy version) CodeForces - 1249D1(贪心+差分)

    题意 给多组线段,而每一个点的覆盖次数不超过K,每次可去除一个线段,问最少去多少线段以及线段的位置. The only difference between easy and hard version ...

  7. leetcode 区间问题

    文章目录 304. 二维区域和检索 - 矩阵不可变[前缀和] 二维前缀和 二维前缀和 一维前缀和 模板 总结 995. K 连续位的最小翻转次数[贪心.差分.滑动窗口]!!! 贪心差分 滑动窗口 总结 ...

  8. 2021ICPC欧洲东南部区域赛题解ACFGJKLN

    2021ICPC欧洲东南部区域赛题解ACFGJKLN A. King of String Comparison 题意 给定两长度为n(1≤n≤2e5)n\ \ (1\leq n\leq 2\mathr ...

  9. 2022 ICPC 南京站

    2022 ICPC 南京 三题 铜 第一块 ICPC 牌子 用了两个小时就结束了,后面三小时 D和M一起开 非常可惜一题都没过,D题我们用了主席树加二分 复杂度是 nlogn2nlogn^2nlogn ...

  10. vue获取input的属性_Vue中自动获取input焦点

    1.给input属性添加autofocus属性,缺点autofocus 在移动版 Safari 上不工作 2.Vue官网给出的解决办法 // 注册一个全局自定义指令 `v-focus` Vue.dir ...

最新文章

  1. POJ--2391--Ombrophobic Bovines【分割点+Floyd+Dinic优化+二分法答案】最大网络流量
  2. 面试都在问的微服务,一文带你彻底搞懂!
  3. css3 animation(左右摆动) (放大缩小)
  4. 为什么小程序裂变,最终都变成了利益驱动?
  5. jenkins通过Execute shell中通过scp/ssh命令,将一台机器中项目远程拷贝到另外一台机器,修改文件中内容
  6. nodejs+webpack+vue以及npm安装对应的库
  7. python怎么操作_python怎么操作mysql
  8. 全球以太网交换机和路由器市场:谁领跑?
  9. 在布局空间标注的尺寸量不对_CAD解决布局标注尺寸不对问题 及快捷键混乱问题...
  10. php页面源代码怎么优化,php代码优化及php相关问题总结
  11. Delphi中实现ListView滚动条的换肤方案
  12. 使用c语言的多学科协同仿真软件,Multisim10软件在单片机C语言协同仿真中应用.pdf...
  13. 21岁雅虎卖身引发一波怀旧 市值曾高达1250亿美元
  14. 基于java springboot记账本微信小程序源码(毕设)
  15. [乐意黎原创]JS函数声明、 函数表达式与立即执行函数的理解与执行顺序
  16. java string替换最后一个字符_sed替换每行最后一个字符
  17. 终结者2018java游戏_如何评价电影《终结者 2018》?
  18. Ubuntu10.10下安装Tor,PolipoVidalia
  19. 【成都站报名】美团点评、蚂蚁金服、腾讯专家共论前端热点技术
  20. 创业的成功率只有10%?错,有的人可以高达90%!为什么?

热门文章

  1. 通过爬虫获取免费IP代理,搭建自己的IP池(http)
  2. 服务器部署dble全流程
  3. 机械制造技术类毕业论文文献都有哪些?
  4. 股票python量化交易026-数据回测的概念以及现有框架
  5. 两边双虚线是什么意思_高速公路上有些路段画有双虚线是什么意思
  6. CAN唤醒硬件方案和软件分析
  7. 论文翻译:Associative Embedding:End-to-End Learning for Joint Detection and Grouping
  8. 多极神经元切片手绘图,神经组织切片手绘图片
  9. 计算机主机风扇安装方法,散热风扇怎么装 电脑机箱散热风扇安装图解教程 (全文)...
  10. 迭代器模式之看芒果台还是央视nie?