Face The Right Way

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 6259 Accepted: 2898

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 M

Sample Input

7

B

B

F

B

F

B

B

Sample 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)


解题心得:

  1. 题意就是给你一列牛,有的牛朝前面有的牛朝后面,你可以选择一段区间,让区间内的牛方向反转,现在你要选择一个区间,要总的反转次数最少让所有的牛都面向前面。
  2. 最暴力的方法就是递归枚举,大概O(2^n)的复杂度,不现实,然后观察会发现,其实对于反转操作来说顺序是没有意义的,这样如果我们每次考虑最左边的牛,那么每次改变之后左边的牛的方向将不会变化,只有右边的牛的方向会变化。那么可以选择从左边开始,枚举区间长度,每次从最左边向右遍历,将区间里面的牛反转,操作下来复杂度大概是O(n^3)。但是想一下,一个牛两次反转之后就等于没有变化,这样我们只需要知道每一头牛翻转了多少次就行了,不必一头一头的操作。这样就可以记录在当前牛之前对这个牛操作了多少次,就可以减去一层的复杂度。这个时候复杂度就变成了O(n^2),可以过了。

#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <climits> using namespace std;
const int maxn = 5e3 + 10; int add[maxn],dir[maxn]; void init(int &n) {
scanf("%d", &n);
char s[5];
for (int i = 1; i <= n; i++) {
scanf("%s", s);
if (s[0] == 'B')
dir[i] = 1;
}
} void solve(int n) {
int sum,cnt,Min = INT_MAX,len;
for (int k = 1; k <= n; k++) {
memset(add,0,sizeof(add));
cnt = sum = 0;
for (int i = 1; i <= n - k + 1; i++) {
if ((sum + dir[i]) % 2 == 1) {
add[i] = 1;
cnt++;
}
sum += add[i];
if (i - k + 1 >= 1)
sum -= add[i-k+1];
}
bool flag = false;
for(int i=n-k+2;i<=n;i++) {//检验剩下的k-1头牛是否符合向前
if((sum+dir[i])%2 == 1) {
flag = true;
break;
}
if(i-k+1 >= 1)
sum -= add[i-k+1];
}
if(flag)
continue;
if(cnt < Min) {
Min = cnt ;
len = k;
}
}
printf("%d %d\n",len,Min);
} int main() {
int n;
init(n);
solve(n);
}

POJ:3276-Face The Right Way(线性反转)的更多相关文章

  1. 反转(开关问题) POJ 3276

    POJ 3276 题意:n头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全部面朝前方.问:求最小操作m,再此基础上求k. 题解:1.5000头 ...

  2. poj 3276(反转)

    传送门:Problem 3276 参考资料: [1]:挑战程序设计竞赛 先献上AC代码,题解晚上再补 题意: John有N头牛,这些牛有的头朝前("F"),有的朝后("B ...

  3. POJ 3276 Face The Right Way 反转

    大致题意:有n头牛,有些牛朝正面,有些牛朝背面.现在你能一次性反转k头牛(区间[i,i+k-1]),求使所有的牛都朝前的最小的反转次数,以及此时最小的k值. 首先,区间反转的顺序对结果没有影响,并且, ...

  4. POJ 3276 Face The Right Way(反转)

      Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6038   Accepted: 2 ...

  5. POJ 3276 (开关问题)

    题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...

  6. Enum:Face The Right Way(POJ 3276)

    面朝大海,春暖花开 题目大意:农夫有一群牛,牛排成了一排,现在需要把这些牛都面向正确的方向,农夫买了一个机器,一次可以处理k只牛,现在问你怎么处理这些牛才可以使操作数最小? 这道题很有意思,其实这道题 ...

  7. poj 2533 Longest Ordered Subsequence(线性dp)

    题目链接:http://poj.org/problem?id=2533 思路分析:该问题为经典的最长递增子序列问题,使用动态规划就可以解决: 1)状态定义:假设序列为A[0, 1, .., n],则定 ...

  8. POJ 3276 Face The Right Way 翻转(开关问题)

    题目:Click here 题意:n头牛排成一列,F表示牛面朝前方,B表示面朝后方,每次转向K头连续的牛的朝向,求让所有的牛都能面向前方需要的最少的操作次数M和对应的最小的K. 分析:一个区间反转偶数 ...

  9. POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)

    分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...

随机推荐

  1. 线程队列queue

    队列queue 队列用于线程之间安全的信息交换 队列和列表的区别:队列里的信息get()后就没了,而列表获取数据则是copy,原列表里的值还在 使用前先实例化队列 q = queue.Queue(ma ...

  2. ansible使用7-Loops

    Standard Loops with_items - name: add several users user: name={{ item }} state=present groups=wheel ...

  3. js关于cookie的各种方法

    //删除cookiedelCookie("GroupName");//s20是代表20秒//h是指小时,如12小时则是:h12//d是天数,30天则:d30setCookie(&q ...

  4. JavaScript 面向对象编程(二):继承

    Javascript面向对象编程(二):构造函数的继承 这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继 ...

  5. vs2008使用mysql链接错误

    原因是因为安装了64位的mysql,而开发工具室32位的,需要安装32位的开发库就可以了

  6. java调用dll库

    1.dll叫动态链接库,作用是用某种语言封装好某些函数生成可供不同语言调用的.dll文件,通常是用C++编写生成,因为C++可以对很多硬件操作方便而其他高级语言不行 2.dll生成参考:http:// ...

  7. IOS 九宫格算法

    @interface ViewController () @property (nonatomic,strong) NSArray *apps; //获取.plist数据 @end @implemen ...

  8. Python动态类型简单介绍

    动态类型以及它提供的多态性,无疑是Python语言简洁性和灵活性的基础.   一.变量 <1>变量创建 一个变量a.当代码第一次给它赋值时就创建了它,之后的赋值将会改变已创建的变量名的值. ...

  9. 1993: C语言实验——最值

    1993: C语言实验——最值 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1541  Solved: 727[Submit][Status][Web ...

  10. Visual Studio 2013 ReportViewer Control

    最近需要给学生讲报表,.NET的自然就是选择RDLC了. 因为学生比赛是用Visual Studio 2013,所以我在自己的笔记本上安装了Visual Studio 2013(平常是用2010),安 ...