poj 3276 Face the right way(反转)
来源:互联网 发布:nginx websocket 配置 编辑:程序博客网 时间:2024/06/15 10:31
题目链接:点击打开链接
可以先将问题缩小化,假设K是确定的,如果第一个牛的方向有问题,那么从这个牛开始到后面K个牛的方向必须反转,反之这个区间必须不能反转,这样第二个牛的状态也知道了,也就确定了第2个牛到第K+1个牛是否需要反转。一直递推下去,当剩余牛数小于K时,判断一下这些牛是不是都是方向向前的,如果不是说明这个K不合法,如果是就知道这个K对应的最少反转次数。
这样用上面的算法枚举K,如果当前K合法就更新最少反转次数。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[5005];int f[5005];int N;int solve(int K){ //cout<<'K'<<K<<endl; memset(f,0,sizeof(f)); int cur=0; int res=0; for(int i=1;i<=N-K+1;i++){ if((cur+a[i])%2){ res++; f[i]=1; //cout<<i<<endl; } cur+=f[i]; if(i-K>=0){ cur-=f[i-K+1]; } } for(int i=N-K+2;i<=N;i++){ if((a[i]+cur)%2){ return -1; } cur+=f[i]; if(i-K>=0){ cur-=f[i-K+1]; } } return res;}int main(){ scanf("%d",&N); for(int i=1;i<=N;i++){ char c[2]; scanf("%s",c); if(c[0]=='B'){ a[i]=1; } } int M,K; int Min=10000000,res; for(K=1;K<=N;K++){ int tmp=solve(K); if(tmp==-1) continue; if(tmp<Min){ Min=tmp; res=K; } } printf("%d %d",res,Min); 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(反转问题)
- poj 3276Face The Right Way—反转(开关问题)
- pOJ 3276 Face The Right Way【思维 反转开关】
- poj 3276 Face The Right Way 挑战150 反转
- 反转(开关)问题(Face the right way POJ NO.3276)
- 150_反转(开关问题) Face the right way (POJ No.3276)
- POJ 3276 Face The Right Way(一维反转问题)
- POJ 3276 Face The Right Way [反转 (贪心)] 《挑战程序设计竞赛》 3.2
- 【POJ】POJ 3276 Face The Right Way (开关问题)
- poj 3276 Face The Right Way
- poj 3276 Face The Right Way
- 细说CSS中的display属性
- spring在web容器启动时执行初始化方法
- Duilib学习笔记《04》— 窗体显示
- 关于错误:"ORA-04091: table is mutating, trigger/function may not see it"的分析(触发器操作自身表)
- 排序2:插入排序(折半插入排序)
- poj 3276 Face the right way(反转)
- hdu4549
- C++局部变量指针保持在List,Vector中使用的问题
- C# 委托实例(跨窗体操作控件)
- 链表,逆波兰表达式
- Duilib学习笔记《03》— 控件使用
- Add a Sensor to a Robot
- wordpress中把Kindeditor和Syntaxhighlighter整合在一起
- POJ 1502 MPI Maelstrom(最短路)