题目描述

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 \le N \le 5000\)。每头牛或者向前或者向后。为了让所有牛都面向前方,农夫每次可以将 \(K\) 头连续的牛转向 \(1 \le K \le N\),求使操作次数最小的相应 \(K\) 和最小的操作次数 \(M\)。\(F\) 为朝前,\(B\) 为朝后。

请在一行输出两个数字 \(K\) 和 \(M\),用空格分开。

输入格式

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

样例输入 #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)

分析

这道题的思想跟acwing 95.费解的开关有点像。

就是按照顺序遍历一遍, 前面的状态已经确定,后状态的改变就不会对前面产生影响。

那么只要先枚举一遍修改区间的长度,遍历数组,遇到 0 就改变后面一定长度的区间。如果说要改变的地方超过了数组总长度,这个方案就是不行的。枚举长度+遍历数组+修改,时间复杂度\(O(n^3)\) n = 5000,显然需要优化。枚举长度和遍历数组不好优化,修改的话优化方法较多,比如差分,树状数组啥的。

差分只要修改两个点,就可以将两点间的区间修改,但是求值又是\(O(n)\),但是这题也不需要求值,实际上看题解感觉这差分数组跟标记数组差不多。

颠倒两次相当于没变(虽然众所周知,还是有提的必要)

#include<iostream>
#include<cstring>
using namespace std; #define N 5010 bool cha[N];
int n, a[N];
int now, ans1 = 0x3f3f3f3f, ans2, tot;
char ch; int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> ch;
if(ch == 'F')
a[i] = 1;
}
for(int k = 1; k <= n; k ++)//遍历区间长度
{
memset(cha, 0, sizeof(cha));
int flag = 1, tot = 0, now = 0;//now表示这一段区域是否翻转
for(int i = 1; i <= n; i ++)
{
now ^= cha[i];//遇到变化区间的末尾时,再变回来
if(a[i] ^ now == 0)
{
if(i + k - 1 > n)//超出范围
{
flag = 0;
break;
}
tot ++;
cha[i + k] ^= 1;
now ^= 1;
}
}
if(flag == 1)
{
if(tot < ans1)//记录一下再少变化数量
{
ans1 = tot;
ans2 = k;
}
}
}
cout << ans2 << " " << ans1 << endl;
return 0;
}

luogu P2882 [USACO07MAR]Face The Right Way G的更多相关文章

  1. 洛谷 P2882 [USACO07MAR]Face The Right Way G

    题目传送门 题目描述 Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing ...

  2. [USACO07MAR]Face The Right Way G

    发现选定一个长度后,怎么翻转是固定的. 那我们直接选定一个长度去操作就行. 优化操作过程 类似于堆里打持久化标记一样的感觉. [USACO07MAR]Face The Right Way G // P ...

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

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

  4. USACO07MAR Face The Right Way G 差分

    题目链接 https://www.luogu.com.cn/problem/P2882 分析 这个题来看的话好像有点难下手,不如再去读一遍题 N遍,发现一句话很重要Each time the mach ...

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

  6. luogu P2973 [USACO10HOL]Driving Out the Piggies G 驱逐猪猡

    luogu LINK:驱逐猪猡 bzoj LINK:猪猪快跑 问题是在1时刻有个炸蛋在1号点 这个炸弹有p/q的概率爆炸 如果没有爆炸 那么会有1/di的概率选择一条边跳到另外一个点上重复这个过程. ...

  7. P2882 [USACO07MAR]Face The Right Way [贪心+模拟]

    题目描述 N头牛排成一列1<=N<=5000.每头牛或者向前或者向后.为了让所有牛都 面向前方,农夫每次可以将K头连续的牛转向1<=K<=N,求操作的最少 次数M和对应的最小K ...

  8. 『题解』洛谷P2296 寻找道路

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 在有向图\(\mathrm G\)中,每条边的长度均为\(1\),现给定起点和终点 ...

  9. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

随机推荐

  1. 全新升级的AOP框架Dora.Interception[2]: 基于&ldquo;约定&rdquo;的拦截器定义方式

    Dora.Interception有别于其他AOP框架的最大的一个特点就是采用针对"约定"的拦截器定义方式.如果我们为拦截器定义了一个接口或者基类,那么拦截方法将失去任意注册依赖服 ...

  2. VR技术的应用领域有哪些?

    5G时代已经到来,新技术的商用不仅能够为用户带来更快的网络连接速度,那些依靠网速的提升,而得以更快发展的科技企业,也迎来了新的发展契机. 这其中,尤为值得关注的是VR领域. 谈起VR,这并不算一个陌生 ...

  3. 手写网站服务器~用Python手动实现一个简单的服务器,不借助任何框架在浏览器中输出任意内容

    写在前面的一些P话: 在公司网站开发中,我们往往借助于Flask.Django等网站开发框架去提高网站开发效率.那么在面试后端开发工程师的时候,面试官可能就会问到网站开发的底层原理是什么? 我们不止仅 ...

  4. 任何快速查询IP归属地

    最近公司项目需要做一个IP归属地查询的功能,想着如果用现成的API就可以大大提高开发效率,所以在网上的API商店搜索了一番,发现了 APISpace,它里面的IP归属地API非常符合我的开发需求.   ...

  5. 沁恒CH32V103C8T6(二): Linux RISC-V编译和烧录环境配置

    目录 沁恒CH32V103C8T6(一): 核心板焊接和Windows开发环境配置 沁恒CH32V103C8T6(二): Linux RISC-V编译和烧录环境配置 硬件准备 CH32V103 开发板 ...

  6. 【C++】从设计原理来看string类

    1.一些C++基础知识 模板类string的设计属于底层,其中运用到了很多C++的编程技巧,比如模板.迭代器.友元.函数和运算符重载.内联等等,为了便于后续理解string类,这里先对涉及到的概念做个 ...

  7. LCA——树上倍增

    首先,什么是LCA? LCA:最近公共祖先 祖先:从当前点到根节点所经过的点,包括他自己,都是这个点的祖先 A和B的公共祖先:同时是A,B两点的祖先的点 A和B的最近公共祖先:深度最大的A和B的公共祖 ...

  8. nginx的高级用法

    一.根据url中的参数来确定缓存的key set_by_lua_block $dataArg { local enc = ngx.req.get_uri_args()["enc"] ...

  9. 判断语句的嵌套和switch判断

    Scanner scanner = new Scanner(System.in);int i = scanner.nextInt();int i2 = scanner.nextInt();if (i& ...

  10. php命名空间粗解

    // 创建命名空间Articlenamespace Article;class Comment { } // 创建命名空间MessageBoardnamespace MessageBoard;clas ...