洛谷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 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,求操作的对应的最小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:
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)
这道题可以枚举每一种情况,然后O(n)判断是否可行。用all来表示当前的所有操作对这个点所产生的影响,并时刻更新。别忘了最后判断是否已经全部为F。
#include <bits/stdc++.h>
#define int long long
#define For(i, a, b) for (register int i = a; i <= b; i++)
using namespace std;
int n, a[5500], ans, rev[5500], all, tot, mn = 0x3f3f3f3f, mnat;
char ch;
bool ok(int x) {
memset(rev, 0, sizeof rev);
all = 0, tot = 0;
For(i, 1, n - x + 1) {
if ((a[i] + all) % 2 == 1) {
rev[i] = 1;
tot++;
}
all += rev[i];
if (i - x + 1 >= 1)
all -= rev[i - x + 1];
}
if (tot > mn)
return 0;
For(i, n - x + 2, n) {
if ((a[i] + all) % 2 == 1)
return 0;
if (i - x + 1 >= 1)
all -= rev[i - x + 1];
}
return 1;
}
signed main() {
cin >> n;
For(i, 1, n) {
cin >> ch;
a[i] = (ch == 'F' ? 0 : 1);
}
For(i, 1, n) {
if (ok(i) && tot < mn) {
mn = tot;
mnat = i;
}
}
cout << mnat << " " << mn << '\n';
return 0;
}
洛谷P2882 [USACO07MAR]面对正确的方式Face The Right Way(贪心)的更多相关文章
- bzoj1704 / P2882 [USACO07MAR]面对正确的方式Face The Right Way
P2882 [USACO07MAR]面对正确的方式Face The Right Way $n<=5000$?枚举翻转长度,顺序模拟就ok了 对于每次翻转,我们可以利用差分的思想,再搞搞前缀和. ...
- [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 f ...
- 洛谷 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 ...
- 洛谷P1084 疫情控制(NOIP2012)(二分答案,贪心,树形DP)
洛谷题目传送门 费了几个小时杠掉此题,如果不是那水水的数据的话,跟列队的难度真的是有得一比... 话说蒟蒻仔细翻了所有的题解,发现巨佬写的都是倍增,复杂度是\(O(n\log n\log nw)\)的 ...
- 洛谷P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L…
P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L… 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many simi ...
- 洛谷 P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L…
P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L… 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many simi ...
- 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)
P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...
- 洛谷P1360 [USACO07MAR]黄金阵容均衡题解
题目 不得不说这个题非常毒瘤. 简化题意 这个题的暴力还是非常好想的,完全可以过\(50\%\)的数据.但是\(100\%\)就很难想了. 因为数据很大,所以我们需要用\(O(\sqrt n)\)的时 ...
- 洛谷P2881 [USACO07MAR]排名的牛Ranking the Cows(bitset Floyd)
题意 题目链接 Sol 显然如果题目什么都不说的话需要\(\frac{n * (n - 1)}{2}\)个相对关系 然后求一下传递闭包减掉就行了 #include<bits/stdc++.h&g ...
随机推荐
- C# 文件搬运(从一个文件夹Copy至另一个文件夹)
时常我们会遇到文件的复制.上传等问题.特别是自动化生产方面,需要对机台抛出的档案进行搬运.收集,然后对资料里的数据等进行分析,等等. Winform下,列举集中较常见的档案的搬运. 1 private ...
- 《Linux就该这么学》培训笔记_ch23_使用OpenLDAP部署目录服务
<Linux就该这么学>培训笔记_ch23_使用OpenLDAP部署目录服务 文章主要内容: 了解目录服务 目录服务实验 配置LDAP服务端 配置LDAP客户端 了解目录服务 其实目录可以 ...
- Git/SVN提交代码规范
feat - 新功能 feature fix - 修复 bug docs - 文档注释 style - 代码格式(不影响代码运行的变动) refactor - 重构.优化(既不增加新功能,也不是修复b ...
- CI框架从哪里看起?CI框架怎么开始学习,CI的初始设置
很多朋友不知道CI框架从哪里开始学起,想学一个新的框架其实并不难.只要你认真研究,自习摸索都很简单! 概述和基本配置参数 配置CI: application/config/config.php:14配 ...
- linux ------ 在Vm 安装 centos系统
------------- 简介 熟悉的操作系统*(android apple windows) 主要分类 1.应用领域(桌面.服务器.嵌入式) 2.源码开放程度(开源.闭源) 3.所支持的用户数 ...
- python 基础 ---- 面向对象
------ 面向对象的思想 三个基本特征: 封装(封装属性方法可以减少耦合)继承(可以抬高开发效率) 多态 主要包括 : 类 : 描述具有相同的属性和方法的对象的集合 变量: 类变量/ 成 ...
- Win10开启上帝模式
1.新建一个文件夹2.修改文件夹名字为 上帝模式.{ED7BA470-8E54-465E-825C-99712043E01C}
- How to let your website login with domain account when using IIS to deploy it?
如何让你的网站以域账号登录 Select your website in IIS Manager, open Authentication, enable Windows Authentication ...
- C 编程环境搭建 Window 篇
前言 - 简介 我们在写代码的过程中, 不可避免的重度依赖所处的开发环境. 本文重点带大家在 Window 搭建 C 简单控制台项目. 当作存档, 用于记录项目搭建各种重复操作. 在详细过程之前, ...
- 一段代码看 Java 引用类型
Java 中的操作数(不知道叫什么,相对于 bytecode 而言,类似 CPU 的操作码和操作数)分为值类型和引用类型: 值类型就是直接存储最终数值的,如 char, int, float, dou ...