Face The Right Way poj ( 开关问题)
来源:互联网 发布:超a鞋淘宝 编辑:程序博客网 时间:2024/06/06 12:56
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
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#include <iostream>using namespace std; int n; int dir[5010];//牛的方向(0:F 1:B) int f[5010]; //i是否翻转 int calc(int K) { memset(f,0,sizeof(f)); int res=0; int sum=0;//[i,i+k-1]中f[i]的和 for(int i=0;i+K<=n;i++) { //牛面朝后方翻转 if((dir[i]+sum)%2) { res++; f[i]=1; } sum+=f[i]; if(i-K+1>=0)//减去不属于[i,i+k-1]的翻转次数 sum-=f[i-K+1]; } //判断最后的牛是否有面朝后方的 for(int i=n-K+1;i<n;i++) { if((dir[i]+sum)%2)return -1; if(i-K+1>=0)sum-=f[i-K+1]; } return res; } void solve() { char a; scanf("%d%*c",&n); int K=1,M=n; for(int i=0;i<n;i++) { scanf("%c%*c",&a); if(a=='F') dir[i]=0; else dir[i]=1; } for(int k=1;k<=n;k++)//依次遍历k是否成立 { int m=calc(k); if(m>0&&M>m) { M=m; K=k; } } printf("%d %d\n",K,M); }int main(){ solve(); return 0;}
- 【POJ】POJ 3276 Face The Right Way (开关问题)
- POJ 3276 Face The Right Way(开关问题)
- POJ - 3276 Face The Right Way(开关问题)
- POJ 3276 Face The Right Way 反转(开关问题)
- POJ 3276:Face The Right Way (开关问题)
- Face The Right Way poj ( 开关问题)
- POJ 3276 Face The Right Way 开关问题
- Face The Right Way poj 3276 开关问题
- poj 3276 Face The Right Way【开关问题】
- poj 3276Face The Right Way—反转(开关问题)
- poj 3276 Face The Right Way(开关问题)
- POJ 3276 Face The Right Way【开关问题】
- POJ 3276 Face The Right Way——开关问题
- POJ 3276 Face The Right Way(开关转换)
- poj3276 Face The Right Way(反转 开关问题)
- 反转(开关)问题(Face the right way POJ NO.3276)
- 150_反转(开关问题) Face the right way (POJ No.3276)
- POJ 3276-Face The Right Way(牛面向前方-开关问题)
- android学习二十三(解析json格式数据)
- 学习阶段总结
- Snd CAN frame
- 最佳工程实践—思维利器OmniGraffle
- LCD典型问题及解决方案
- Face The Right Way poj ( 开关问题)
- jmeter(补充中...)
- JNI----呵呵三部曲
- 使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中<meta-data>变量的值
- 如何在 ASP.NET MVC 中集成 AngularJS(3)
- 机器学习的一些基本概念——参考《机器学习》
- VC----MFC消息映射机制详解
- 未来工厂——电器行业ERP案例
- 最后补充一个RxJava博客:walfud 彻底搞懂 RxJava 系列