Face The Right Way
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2193   Accepted: 1039

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)

Source

 
反转法解
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define MAX_N 5005 int N;
int dir[MAX_N],f[MAX_N]; int cal(int K) {
memset(f,,sizeof(f));
int sum = ;
int res = ;
for(int i = ; i + K - <= N; ++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) % ) {
//printf(" i = %d sum = %d dir = %d\n",i,sum,dir[i]);
return -;
}
if(i - K + >= ) sum -= f[i - K + ];
} return res;
} void solve() {
int ansm,ansk,t;
for(int k = ; k <= N; ++k) {
//printf("cal = %d\n",cal(k));
if((t = cal(k)) >= && t < ansm) {
ansm = t;
ansk = k;
}
} printf("%d %d\n",ansk,ansm);
}
int main()
{
// freopen("sw.in","r",stdin);
scanf("%d",&N);
for(int i = ; i <= N; ++i) {
char ch[];
scanf("%s",&ch);
if(ch[] == 'F') dir[i] = ;
else dir[i] = ;
//printf("%c",ch);
} solve(); return ;
}

POJ 3276的更多相关文章

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

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

  2. POJ 3276 (开关问题)

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

  3. poj 3276(反转)

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

  4. POJ 3276 Face The Right Way 反转

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

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

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

  6. Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276

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

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

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

  8. 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 ...

  9. POJ 3276 Face The Right Way(前缀和优化)

    题意:有长度为N的01串,有一个操作可以选择连续K个数字取反,求最小的操作数和最小的K使得最后变成全1串.(N<=5000) 由于K是不定的,无法高斯消元. 考虑枚举K,求出最小的操作数. 显然 ...

随机推荐

  1. 263. Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  2. UVA 10970 第一次比赛 D题 (后面才补的)

    Mohammad has recently visited Switzerland. As heloves his friends very much, he decided to buy somec ...

  3. OpenGL 纹理贴图

    前一节实例代码中有个贴图操作. 今天就简单说明一下纹理贴图... 为了使用纹理贴图.我们首先需要启用纹理贴图功能. 我们可以在Renderer实现的onSurfaceCreated中定义启用: // ...

  4. spring使用JdbcDaoSupport中封装的JdbcTemplate进行query

    1.Dept package cn.hxex.springcore.jdbc; public class Dept { private Integer deptNo; private String d ...

  5. 各设备如何清理dns缓存

    Windows 按下 Windows+R 键,运行 cmd ,在命令提示符运行命令 ipconfig /flushdns OS X 10.10 在[应用程序][实用工具][终端]运行命令 sudo d ...

  6. 条款20:以const-reference传递替换by-value传递

    缺省情况下,C++中函数参数的传递方式为by-value.即函数都是以实际参数的副本进行传递,而函数返回的也是一个副本.考虑如下实例程序: #include <iostream> clas ...

  7. 为边框应用图片 border-image

    为边框应用图片 border-image 顾名思义就是为边框应用背景图片,它和我们常用的background属性比较相似.例如: background:url(xx.jpg) 10px 20px no ...

  8. Xamarin.Android之转换,呼叫,查看历史纪录

    Xamarin.Android之转换,呼叫,查看历史纪录 E文文章. 功能:能将输入的字母转换成相应的数字.并且能呼叫出去.能查看呼叫的历史纪录. 界面代码如下: <?xml version=& ...

  9. 【Same Tree】cpp

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  10. C#基础——三元表达式

    采用三元操作符对?:对表达式进行运算,这种操作符比较特别,因为它有三个操作对象,但它确实属于操作符的一种,它最终也会生成一个值.其表达式采取下述形式: boolean-exp ? value0 : v ...