Face The Right Way(POJ 3276)
- 原题如下:
Face The Right Way
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6708 Accepted: 3124 Description
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.
Input
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.Output
Line 1: Two space-separated integers: K and MSample Input
7
B
B
F
B
F
B
BSample Output
3 3
Hint
For K = 3, the machine must be operated three times: turn cows (1,2,3), (3,4,5), and finally (5,6,7) - 题解:对于一个特定的K如何求出让所有的牛面朝前方的最小操作次数?首先,交换区间反转的顺序对结果是没有影响的,另外,可以知道对同一个区间进行两次以上的反转是多余的,由此,问题就转化成了求需要反转的区间的集合。先考虑一下最左端的牛,因为包含这头牛的区间只有一个,所以如果这头牛面朝前方,我们就能知道这个区间不需要反转,反之如果这头牛面朝后方,对应的区间就必须进行反转了,而且在此之后的最左区间就再也不需要考虑了,这样一来通过首先考虑最左端的牛,问题的规模就缩小了1,不断地重复下去,就可以无需搜索求出最少所需反转次数了。通过前面的分析可以知道,忽略掉对同一个区间重复反转这类多余操作之后,只要存在让所有的牛都朝前的方法,那么操作就和顺序无关,可以唯一确定了。对于整个算法的话,我们需要对所有的K都求解一次,对于每个K最坏情况下需要进行N-K+1次的反转操作,每次操作又要反转K头牛,于是总的复杂度是O(N3)。区间反转的部分还可以进行优化,令f[i]:=区间[i,i+K-1]进行了反转的话为1,否则为0,这样在考虑第i头牛时,如果∑(j=i-K+1,i-1)f[j]为奇数的话,则这头牛的方向与起始方向是相反的,否则方向不变,由于∑(j=(i+1)-K+1,i)f[j] = ∑(j=i-K+1,i-1)f[j]+f[i]-f[i-K+1],所以这个和每一次都可以用常数时间计算出来,复杂度就降为了O(N2),就可以在时限内解决问题了。
- 代码:
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <cstring>
#define num s-'0' using namespace std; const int MAX_N=;
const int INF=0x3f3f3f3f;
int N;
int dir[MAX_N], f[MAX_N]; void read(int &x){
char s;
x=;
bool flag=;
while(!isdigit(s=getchar()))
(s=='-')&&(flag=true);
for(x=num;isdigit(s=getchar());x=x*+num);
(flag)&&(x=-x);
} void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
} int calc(int); int main()
{
read(N);
for (int i=; i<N; i++)
{
char d;
scanf("%c", &d);
d=='B'? dir[i]=: dir[i]=;
scanf("%c", &d);
}
int K=,M=N;
for (int k=N; k>=; k--)
{
int m=calc(k);
if (m>= && m<M)
{
M=m;
K=k;
}
}
write(K);putchar(' ');write(M);putchar('\n');
} int calc(int K)
{
memset(f, , sizeof(f));
int res=;
int sum=;
for (int i=; i<=N-K; i++)
{
if ((dir[i]+sum)% != )
{
res++;
f[i]=;
}
sum+=f[i];
if (i-K+>=) sum-=f[i-K+];
}
for (int i=N-K+; i<N; i++)
{
if ((dir[i]+sum)%!=) return -;
if (i-K+>=) sum-=f[i-K+];
}
return res;
}
Face The Right Way(POJ 3276)的更多相关文章
- 反转(开关问题) POJ 3276
POJ 3276 题意:n头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全部面朝前方.问:求最小操作m,再此基础上求k. 题解:1.5000头 ...
- POJ 3276 (开关问题)
题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...
- poj 3276(反转)
传送门:Problem 3276 参考资料: [1]:挑战程序设计竞赛 先献上AC代码,题解晚上再补 题意: John有N头牛,这些牛有的头朝前("F"),有的朝后("B ...
- POJ 3276 Face The Right Way 反转
大致题意:有n头牛,有些牛朝正面,有些牛朝背面.现在你能一次性反转k头牛(区间[i,i+k-1]),求使所有的牛都朝前的最小的反转次数,以及此时最小的k值. 首先,区间反转的顺序对结果没有影响,并且, ...
- Enum:Face The Right Way(POJ 3276)
面朝大海,春暖花开 题目大意:农夫有一群牛,牛排成了一排,现在需要把这些牛都面向正确的方向,农夫买了一个机器,一次可以处理k只牛,现在问你怎么处理这些牛才可以使操作数最小? 这道题很有意思,其实这道题 ...
- POJ 3276
Face The Right Way Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2193 Accepted: 103 ...
- Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276
Face The Right Way Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2899 Accepted: 133 ...
- POJ 3276 Face The Right Way 翻转(开关问题)
题目:Click here 题意:n头牛排成一列,F表示牛面朝前方,B表示面朝后方,每次转向K头连续的牛的朝向,求让所有的牛都能面向前方需要的最少的操作次数M和对应的最小的K. 分析:一个区间反转偶数 ...
- POJ 3276 Face The Right Way
Description Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing ...
- POJ 3276 Face The Right Way(前缀和优化)
题意:有长度为N的01串,有一个操作可以选择连续K个数字取反,求最小的操作数和最小的K使得最后变成全1串.(N<=5000) 由于K是不定的,无法高斯消元. 考虑枚举K,求出最小的操作数. 显然 ...
随机推荐
- 第一个Mybatis
第一个Mybatis 思路:搭建环境-->导入Mybatis-->编写代码-->测试 1.搭建环境 新建maven工程,配置xml文件 <?xml version=" ...
- 串口线接Linux设备U盘安装系统和直接安装设备接显示屏2种方式不同
Firmware Bug]: TSC_DEADLINE disabled due to Errata; please update microcode to version: 0x22 (or lat ...
- SpringMVC4——视图、视图解析器、国际化
视图.视图解析器.国际化 视图的顶级接口:View 视图解析器:ViewResolver 常见的视图和解析器: InternalResourceView.InternalResourceViewR ...
- start-stop-daemon: matching on world-writable pidfile /var/run/redis/redis-server.pid is insecurefailed
Microsoft Store上 看到最新的Ubuntu 20.04 LTS 已经适配到WSL上了, 于是卸载了老版本 18.04 LTS,安装上了最新版本的. 第一次启动会比较慢,需耐心等待 Ins ...
- Virtuoso 中如何优化 Library Manager 的显示
https://www.cnblogs.com/yeungchie/ 主要从 cds.lib 文件去入手. 假设现在想把 Virtuoso 预装的库整理到 preload 分类(库)中. 首先创建一个 ...
- Spring配置文件中bean标签中init-method和destroy-method和用注解方式配置
Person类: public class Person { private int i = 0; public Person(){ System.o ...
- nvm -- node 多版本管理器
Node.js 越来越热,应用的场景也越来越多. 在开发中,我们可能同时在进行多个 node 项目,而这些不同的项目所使用的 node 版本又是不一样的,或者是要用更新的 node 版本进行试验和学习 ...
- 关于c++11 memory order的理解
关于C++memory_order的理解 看了c++并发编程实战的内存模型部分后,一直对memory_order不太懂,今天在知乎发现了百度的brpc,恰好有关于原子操作的文档,感觉解释的很好.为了加 ...
- 如何使用SPSSAU计算耦合协调度?
什么是耦合协调度模型 耦合协调度模型用于分析事物的协调发展水平.耦合度指两个或两个以上系统之间的相互作用影响,实现协调发展的动态关联关系,可以反映系统之间的相互依赖相互制约程度.协调度指耦合相互作用关 ...
- Selenium中核心属性以及方法
一.操作定位元素 selenium提供了定位元素的API,这些方法都被定义在webDriver类中,需要以find开头, 例如:find_Element_by_id('')