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. JSON语法格式

    一.JSON数据格式 名称/值对 二.JSON值对数据类型 数字    字符串   逻辑值    数组(在方括号中)     对象 (在花括号中)     null eg: { "staff ...

  2. canvas的isPoinInPath API实现交互

  3. 模拟Chrome皮肤

    话不多说,先验货: (原始状态) (最大化状态) (对比图) 为自己鼓掌!!! 哈哈,捣鼓2天终于把这个搞出来了!虽然代码一团糟,但是不难理解! 要实现这个功能需要几个组件:DWM,GDI+ 在实现这 ...

  4. WorkFlow 的 Xaml 中找不到引用类型

    原来还是需要两步走. 1. 在refernece里面应用project或dll. 2. 在xaml的命名空间里面手动添加.

  5. check_mk检测插件 - raid监控

    mk_raidstatus python版本 #!/usr/bin/env python # -*- encoding: utf-8; py-indent-offset: 4 -*- import s ...

  6. ansible使用2-inventory & dynamic inventory

    默认位置 /etc/ansible/hosts 标准 mail.example.com [webservers] foo.example.com bar.example.com [dbservers] ...

  7. Linux修改文件permission可执行属性

    列出文件属性 ls -al 修改文件属性为可读.可写 sudo chmod -c 777 <your file name>

  8. PHP @ at 记号的作用

    看PHP的代码,总有些行前边有@符号,一直不知道是什么意思. 例如dede5.7     @$ni=imagecreatetruecolor($ftoW,$ftoH); 今天用到了,就记一下吧.其实它 ...

  9. 2019.03.13 ZJOI2019模拟赛 解题报告

    得分: \(55+12+10=77\)(\(T1\)误认为有可二分性,\(T2\)不小心把\(n\)开了\(char\),\(T3\)直接\(puts("0")\)水\(10\)分 ...

  10. python 3+djanjo 2.0.7简单学习(二)--创建数据库和模型

    我们紧接上次,这里将建立数据库,创建第一个模型提示:这里我们不需要去一直启动,django会在我们ctrl+s的时候自动刷新并启动服务,很方便吧  1.数据库配置 现在,打开 vote_mysite/ ...