poj3276 Face The Right Way 反转问题
来源:互联网 发布:阿里云更改手机绑定 编辑:程序博客网 时间:2024/05/23 20:10
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
Lines 2..N+1: Line i+1 contains a single character, F or B, indicating whether cow i is facing forward or backward.
Output
Sample Input
7BBFBFBB
Sample Output
3 3
题意:给n头牛的站位:B-向后,F向前。每次反转连续的k个,使其全部向前,求反转次数最小值m及其对应的k值。
分析:
设f[i]为[i,i+k-1]这个区间进行反转,对于第i头牛,只需统计sum = f[i-k+1]--f[i-1]反转的次数,结合本身牛的占位,就可以决定第i头牛是否需要来一次反转。
而对于下一头牛i+1,需要统计sum1 = f[i+1-k+1]--f[i] = sum+f[i]-f[i-k+1]。设置sum变量维护这个数值,可以将每次将f[i]用常熟时间计算出来。
#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <vector>#include <bitset>#include <list>#include <sstream>#include <set>#include <functional>using namespace std;#define INF 0x3f3f3f3f#define MAX 100typedef long long ll;int n;int dir[5001];int f[5001];int K,M;int cal(int k){//计算前n-k+1个值的f[i]memset(f,0,sizeof(f));int sum = 0;int res = 0;for (int i = 0; i+k-1 < n; ++i){if((sum+dir[i])%2 != 0){res++;f[i] = 1;} sum += f[i];if(i-k+1 >= 0) sum -= f[i-k+1];}//检验后面剩余的值for (int i = n-k+1; i < n; ++i){if((sum+dir[i])%2 != 0) return -1;if(i-k+1 >= 0) sum -= f[i-k+1]; } return res;}void solve(){M = n;for (int k = 1; k <= n; ++k){int m = cal(k);if(m>=0 && M>=m) K = k,M = m;}printf("%d %d\n",K,M);}int main(int argc, char const *argv[]){scanf("%d",&n);for (int i = 0; i < n; ++i){char c;cin >> c;if(c == 'B') dir[i] = 1;//向后的为1else dir[i] = 0;//向前为0}solve();}
- poj3276 Face The Right Way 反转问题
- POJ3276 -- Face The Right Way 反转问题
- poj3276 Face The Right Way 反转问题
- POJ3276 Face The Right Way--反转问题初学
- poj3276 Face The Right Way(反转 开关问题)
- poj3276 Face the Right Way
- POJ3276-Face The Right Way
- POJ3276-Face The Right Way
- POJ3276-Face The Right Way
- POJ3276 Face The Right Way
- poj3276 Face The Right Way
- poj3276:Face The Right Way
- poj3276 Face The Right Way模拟
- POJ3276 Face The Right Way 【尺取法】
- POJ3276-Face The Right Wa【反转】
- POJ 3276 Face The Right Way 反转问题 常用技巧
- POJ 3276 Face The Right Way(反转问题)
- poj 3276Face The Right Way—反转(开关问题)
- 学习总结
- 邮件系统使用规则
- 10.开源项目--git commit对象与存储
- Shop项目--1.注册新用户 验证码 前端校验工具validate 发送邮件激活
- 机器学习-人脸识别
- poj3276 Face The Right Way 反转问题
- 通过设置自定义动画实现回到顶部
- 最长子字符串
- JSP介绍和运行机制
- (function($){...})(jQuery)使用
- python3 urllib.request 网络请求操作
- jQuery基本用法二
- scikit-learn 中文文档-线性和二次判别分析-监督学习|ApacheCN
- Tr A