POJ 3276 Face The Right Way【开关问题】
来源:互联网 发布:个人简历制作模板软件 编辑:程序博客网 时间:2024/05/18 10:45
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
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b) memset(a,b,sizeof(a))const int M=1e5+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;int i,j,k,n,m;int dir[M];int f[M];int calc(int k){ int ans=0; int sum=0; ms(f,0); for(int i=0;i+k<=n;i++){ if((dir[i]+sum)%2!=0){ ans++; 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((dir[i]+sum)%2!=0){ return -1; } if(i-k+1>=0) sum-=f[i-k+1]; } return ans;}int main(){ scanf("%d",&n); for(int i=0;i<n;i++){ char c; cin>>c; if(c=='F')dir[i]=0; else dir[i]=1; } int M=n,K=1; for(int i=1;i<=n;i++){ int m=calc(i); if(m>=0&&m<M){ M=m;K=i; } } printf("%d %d\n",K,M); return 0;}
- 【POJ】POJ 3276 Face The Right Way (开关问题)
- POJ 3276 Face The Right Way 开关问题
- Face The Right Way poj 3276 开关问题
- poj 3276 Face The Right Way【开关问题】
- POJ 3276 Face The Right Way(开关问题)
- 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【开关问题】
- POJ 3276 Face The Right Way——开关问题
- Face The Right Way poj ( 开关问题)
- POJ 3276 Face The Right Way(开关转换)
- pOJ 3276 Face The Right Way【思维 反转开关】
- 反转(开关)问题(Face the right way POJ NO.3276)
- POJ 3276 Face The Right Way (开关问题的优化_好题)
- 150_反转(开关问题) Face the right way (POJ No.3276)
- js获取url参数中文乱码问题
- EhCache介绍
- PAT乙级 1031. 查验身份证(15)
- PAT乙级 1032. 挖掘机技术哪家强(20)
- LeetCode--Valid Parentheses
- POJ 3276 Face The Right Way【开关问题】
- PAT乙级 1033. 旧键盘打字(20)
- indigo cv_bridge版本
- Android OpenCV开发环境搭建
- C语言:控制结构与流程图
- PAT乙级 1034. 有理数四则运算(20)
- 解决/usr/lib64/libstdc++.so.6和/lib64/libc.so.6版本过低问题
- AJAX异步请求,底层使用hibernate查询后的数据json封装之前的处理
- Error:Execution failed for task ':testPic:transformResourcesWithMergeJavaResForDebug'.