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. react- 相关

    生命周期方法 组件的生命周期分成三个状态: Mounting:已插入真实 DOM Updating:正在被重新渲染 Unmounting:已移出真实 DOM React 为每个状态都提供了两种处理函数 ...

  2. 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)

    #include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...

  3. JavaScript平台Platypi悄然登场

    几个月前,一个新的JavaScript平台Platypi悄然诞生.它为开发者提供的不仅仅是一套标准的MVC框架而已,由于它是基于TypeScript构建的,因此对开发者而言在熟悉之中透露出与众不同的感 ...

  4. 《spring技术内幕》读书笔记(1)——什么是POJO模式

    今天在看<spring技术内幕>,第一章中多次提到了使用POJO来完成开发,就百度了一下,在此保留 1.     什么是POJO POJO的名称有多种,pure old java obje ...

  5. html:<link> 标签中的 media 属性

    HTML <link> 标签的 media 属性 定义和用法 media 属性规定被链接文档将显示在什么设备上. media 属性用于为不同的媒介类型规定不同的样式. media属性值 ( ...

  6. AttributeError: module 'requests' has no attribute 'get' 遇到了这个错误,是因为我把python关键字做了包名。。。

    初学者总会犯各种低级错误,这是其一,特此记录.

  7. jQuery获取Select选择的Text和Value[转载]

    语法解释:1. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发2. var ch ...

  8. 2017.11.1 微型计算机原理与接口技术-----第七章 中断系统与8237A DMA控制器

    第七章 微型计算机原理与接口技术-----中断系统与8237A DMA控制器 (1)数据传送的两种方式:中断方式和直接存储器存取方式(DMA):中断是微处理器与外部设备交换信息的一种方式:DMA是存储 ...

  9. url 解析

    最近在做一个单页应用,使用AngularJS来处理一些页内路由(哈希#后的路由变化).自然会要解析URL中的参数.使用AngularJS自带的方法$location.search();可以自动将参数整 ...

  10. java mysql多次事务 模拟依据汇率转账,并存储转账信息 分层完成 dao层 service 层 client层 连接池使用C3p0 写入库使用DBUtils

    Jar包使用,及层的划分 c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3 ...