博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Face The Right Way POJ - 3276 (开关问题)
阅读量:5239 次
发布时间:2019-06-14

本文共 3077 字,大约阅读时间需要 10 分钟。

Face The Right Way
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6707   Accepted: 3123

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

Line 1: A single integer: 
N 
Lines 2..
N+1: Line 
i+1 contains a single character, 
F or 
B, indicating whether cow 
i is facing forward or backward.

Output

Line 1: Two space-separated integers: 
K and 
M

Sample Input

7BBFBFBB

Sample Output

3 3

Hint

For 
K = 3, the machine must be operated three times: turn cows (1,2,3), (3,4,5), and finally (5,6,7)
 
题意:N头牛排成了一排,每头牛都超前或者朝后。为了让所有的牛都头朝前需要一个机器,这个机器可以使得连续的K头牛反转(超前变朝后,朝后变朝前),需要反转M次,求最少的操作次数M和对应的最小值K
思路:显然,一头牛如果反转两次就等于没有反转,所以一头牛要是被反转两次就多余了。一排牛,我们从左往右看的话,如果第一头牛是头朝后的,那么势必这头牛需要被反转,如果这头牛是头朝前的,那么我们就可以从第二头牛开始进行反转如果第二头牛它是头朝后的话,比如如果一排牛是BBFBFBB(F向前,B向后),那么我们从左往右看第一头牛它是朝后的,所以需要反转,我们假设此时的K是3,那么反转后是FFBBFBB,由于我们是为了第一头牛朝前,那么反转一次后第一头牛肯定朝前了,接下去我们发现第二头牛头刚好也是朝前,那么我们就继续往下看第三头,第三头头往后,那么就反转345号牛,结果为FFFFBBB,此时,我们去找第四头牛时发现他也是朝前的,那就继续往下,第五头牛朝后,那么就把567给反转。但最后一次反转后要记得验证是否最后一次使得接下去的K个牛都是头朝前的。而K的找寻这里用了暴力,1-N循环了一遍。
 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define INF 0x3f3f3f3f#define eps 1e-10typedef long long ll;const int maxn = 5e3+3;const int mod = 1e9 + 7;int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b);}int N,M;int dir[maxn],f[maxn]; //牛的方向 F:0 B:1int calc(int K){ memset(f,0,sizeof(f)); int res=0,sum=0; for(int i=0;i+K<=N;i++) { if((dir[i]+sum)%2!=0) { res++; f[i]=1; } sum+=f[i]; if(i-K+1>=0) sum-=f[i-K+1]; } for(int i=N-K+1;i
=0) sum-=f[i-K+1]; } return res;}void solve(){ int K=1; int M=N; for(int k=1;k<=N;k++) { int m=calc(k); if(m>=0 && M>m) { M=m; K=k; } } cout<
<<" "<
<
>ch; if(ch=='B') dir[num]=1; else dir[num]=0; // cout<
<<" "; num++; } solve(); return 0;}

 

转载于:https://www.cnblogs.com/smallhester/p/9501247.html

你可能感兴趣的文章
编程面试的10大算法概念汇总
查看>>
Vue
查看>>
python-三级菜单和购物车程序
查看>>
条件断点 符号断点
查看>>
VMware12 + Ubuntu16.04 虚拟磁盘扩容
查看>>
水平垂直居中
查看>>
MySQL简介
查看>>
设计模式之桥接模式(Bridge)
查看>>
jquery的$(document).ready()和onload的加载顺序
查看>>
Python Web框架Django (五)
查看>>
.net学习之继承、里氏替换原则LSP、虚方法、多态、抽象类、Equals方法、接口、装箱拆箱、字符串------(转)...
查看>>
【codevs1033】 蚯蚓的游戏问题
查看>>
【程序执行原理】
查看>>
python的多行注释
查看>>
连接Oracle需要jar包和javadoc文档的下载
查看>>
UVA 10976 - Fractions Again?!
查看>>
Dreamweaver cc新版本css单行显示
查看>>
【android】安卓的权限提示及版本相关
查看>>
JavaScript可否多线程? 深入理解JavaScript定时机制
查看>>
IOS基础学习
查看>>