poj 3276 Face The Right Way
来源:互联网 发布:mac桌面下方图标隐藏 编辑:程序博客网 时间:2024/05/17 01:04
题意:n头牛,不是朝前就是朝后,操作一次可以使连续k头牛反转。求最小操作次数,使所有牛朝前,且k尽量小。
思路:枚举k=1~n。判断取当前的k是否可行,如果可行需要操作多少次。判断方法是累计区间内反转次数,然后检查最后的k头牛是否朝前。
#include<iostream>#include<cmath>#include<queue>#include<vector>#include<algorithm>#include<string.h>#include<cstdio>using namespace std;int a[5010];int n;int f[5010];int calc(int k){int cnt=0;for(int i=1;i<=n;i++)f[i]=0;int ans=0;for(int i=1;i<=n;i++){if(i>k)cnt-=f[i-k];if( !((a[i]+cnt)&1) ){f[i]=1;cnt++;ans++;if(i>n-k+1)return -1;}}return ans;}int main(){cin>>n;char c[2];for(int i=1;i<=n;i++){scanf("%s",c);if(c[0]=='F')a[i]=1;else a[i]=0;}int K,M=5000;for(int k=1;k<=n;k++){int tmp=calc(k);if(tmp!=-1){if(tmp<M){M=tmp;K=k;}}}cout<<K<<" "<<M<<endl;return 0;}
0 0
- 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
- POJ 3276 Face The Right Way
- 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-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@
- poj 3276 Face the right way
- (每日算法)Leetcode--Edit Distance(编辑距离)
- eclipse远程连接hadoop报Cannot run program "chmod": CreateProcess error=2问题
- SPD之解CQ总结
- This system is not registered with ULN.
- Android系统性能调优工具介绍
- poj 3276 Face The Right Way
- Perf -- Linux下的系统性能调优工具介绍
- 走迷宫(Dfs)
- iOS Runtime机制 - 类和对象(1)
- linux git
- Android补间动画属性
- Operating system error number 2 in a file operation
- c++之图的Dijkstra算法!
- 关于malloc函数死机的问题