POJ3276-Face The Right Way
来源:互联网 发布:php生成8位唯一邀请码 编辑:程序博客网 时间:2024/05/12 06:16
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
Hint
Source
题意:n头牛排成一列。每头牛或者向前或者向后。为了让所有牛都面向前方,每次可以将k头连续的牛转向1<=k<=n,求操作的最少次数M和对应的最小k
解题思路:枚举K,然后从左到右扫一遍,判断在该牛的前面第k头牛是否转向,若转向则总转向次数减一,然后判断该牛是否要转向
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <stack>#include <cmath>#include <map>#include <bitset>#include <set>#include <vector>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;char ch[5009];int n,a[5009],visit[5009];int main(){while(~scanf("%d",&n)){for(int i=1;i<=n;i++){scanf("%s",ch+i);if(ch[i]=='F') a[i]=1;else a[i]=0;}int mi=INF,k;for(int i=1;i<=n;i++){int flag=1,sum=0,cnt=0;memset(visit,0,sizeof visit);for(int j=1;j<=n-i+1;j++){if(visit[j-i]&&j-i>=1) sum--;if((a[j]+sum)%2==0) visit[j]=1,sum++,cnt++;}for(int j=n-i+2;j<=n;j++){if(visit[j-i]&&j-i>=1) sum--;if((a[j]+sum)%2==0) flag=0;}if(flag&&mi>cnt) {k=i,mi=cnt;}}printf("%d %d\n",k,mi);}return 0;}
- 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(反转 开关问题)
- bzoj1704/poj3276[Usaco2007 Mar]Face The Right Way自动转身机
- bzoj1704/poj3276[Usaco2007 Mar]Face The Right Way自动转身机
- Face The Right Way
- Face The Right Way
- 【算法题】链表冒泡排序
- mr中的combiner
- 内存监视
- Cookie应用:显示上次访问页面时间
- bzoj 4278: [ONTAK2015]Tasowanie&bzoj 1692: [Usaco2007 Dec]队列变换 后缀数组+贪心
- POJ3276-Face The Right Way
- 线段树
- 用java编程模仿DOS下的dir命令,列出某个目录下的内容
- Kotlin配置与使用
- 彻头彻尾理解 HashMap
- 我的mongodb操作手册
- 浅谈迭代器Iterator
- C# 实现Winform全屏后不遮挡任务栏,显示任务栏
- sping+springmvc+mybatis+shiro,配置文件