POJ:3276-Face The Right Way(线性反转)
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)
解题心得:
- 题意就是给你一列牛,有的牛朝前面有的牛朝后面,你可以选择一段区间,让区间内的牛方向反转,现在你要选择一个区间,要总的反转次数最少让所有的牛都面向前面。
- 最暴力的方法就是递归枚举,大概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(线性反转)的更多相关文章
- 反转(开关问题) POJ 3276
POJ 3276 题意:n头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全部面朝前方.问:求最小操作m,再此基础上求k. 题解:1.5000头 ...
- poj 3276(反转)
传送门:Problem 3276 参考资料: [1]:挑战程序设计竞赛 先献上AC代码,题解晚上再补 题意: John有N头牛,这些牛有的头朝前("F"),有的朝后("B ...
- POJ 3276 Face The Right Way 反转
大致题意:有n头牛,有些牛朝正面,有些牛朝背面.现在你能一次性反转k头牛(区间[i,i+k-1]),求使所有的牛都朝前的最小的反转次数,以及此时最小的k值. 首先,区间反转的顺序对结果没有影响,并且, ...
- POJ 3276 Face The Right Way(反转)
Face The Right Way Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6038 Accepted: 2 ...
- POJ 3276 (开关问题)
题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...
- Enum:Face The Right Way(POJ 3276)
面朝大海,春暖花开 题目大意:农夫有一群牛,牛排成了一排,现在需要把这些牛都面向正确的方向,农夫买了一个机器,一次可以处理k只牛,现在问你怎么处理这些牛才可以使操作数最小? 这道题很有意思,其实这道题 ...
- poj 2533 Longest Ordered Subsequence(线性dp)
题目链接:http://poj.org/problem?id=2533 思路分析:该问题为经典的最长递增子序列问题,使用动态规划就可以解决: 1)状态定义:假设序列为A[0, 1, .., n],则定 ...
- POJ 3276 Face The Right Way 翻转(开关问题)
题目:Click here 题意:n头牛排成一列,F表示牛面朝前方,B表示面朝后方,每次转向K头连续的牛的朝向,求让所有的牛都能面向前方需要的最少的操作次数M和对应的最小的K. 分析:一个区间反转偶数 ...
- POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)
分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...
随机推荐
- JS的函数参数传递为值传递
function setAge(i) { alert(i);//24 i = 18; alert(i);//18,i的改变不会影响外面的age }; var age = 24; setAge(age) ...
- Java Knowledge series 7
Pepole who make a greate contribution on common libaraies deserve our respect. Component(Widget) / S ...
- SQL Union和Union All使用方法
格式: [SQL 语句 1]UNION [SQL 语句 2] 对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: select ID,NAME from A UNION select I ...
- 微信公众平台开发——helloworld
威信公众平台有两种模式:编辑模式 和 开发模式. 普通的功能可以通过编辑模式来搞定.开发模式具有更多的功能.让我们来使用开发模式开发helloword吧 步骤如下: 1.先注册一个公众号(https: ...
- Java 空对象设计模式(Null Object Pattern) 讲解
转自:http://www.cnblogs.com/haodawang/articles/5962531.html 有时候我们的代码中为避免 NullPointerException 会出现很多的对N ...
- springboot项目搭建:结构和入门程序
Spring Boot 推荐目录结构 代码层的结构 根目录:com.springboot 1.工程启动类(ApplicationServer.java)置于com.springboot.build包下 ...
- python3线程介绍01(如何启动和调用线程)
#!/usr/bin/env python# -*- coding:utf-8 -*- import osimport time,randomimport threading # 1-进程说明# 进程 ...
- 正则表达式转换python2的print为python3风格
直接查找 print ([^\n\(]*)替换为 print($1)
- *387. First Unique Character in a String (linkedhashmap + string) debug manunally instead of using leetcode
The ability to debug and overall thinking need to improve Given a string, find the first non-repeati ...
- Python 类的高级属性(可选)
1.slots实例:限制类的实例有合法的属性集,只有__slots__属性列表中的属性才可能成为实例属性. 对象的实例通常没有一个属性字典,可以在__slots__列表中包含一个属性字典__dict_ ...