poj3276
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5121 | Accepted: 2374 |
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
7
B
B
F
B
F
B
B
Sample Output
3 3
Hint
Source

ac代码
import java.util.Scanner; public class Main{ /**
* @param args
*/
static int dir[]=new int[5000+2];
static int n;
static int f[]=new int[5000+2];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
n=scan.nextInt();
scan.nextLine();
for(int i=0;i<n;i++){
String str=scan.nextLine();
if(str.equals("F")){
dir[i]=0;
}else{
dir[i]=1;
}
}
sovle();
}
private static void sovle() {
// TODO Auto-generated method stub
int M=n,K=1;
for(int k=1;k<=n;k++){
int m=calc(k);
if(m>0&&M>m){
M=m;
K=k;
}
}
System.out.println(K+" "+M);
}
private static int calc(int k) {
// TODO Auto-generated method stub
init();
int res=0;
int 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<n;i++){
if((dir[i]+sum)%2!=0){
return -1;
}
if(i-k+1>=0){
sum-=f[i-k+1];
}
}
return res;
}
private static void init() {
// TODO Auto-generated method stub
for(int i=0;i<n;i++){
f[i]=0;
}
} }
poj3276的更多相关文章
- poj3276 Face The Right Way(反转问题,好题)
https://vjudge.net/problem/POJ-3276 首先意识到,对一个区间进行两次及以上的反转是没有意义的,而且反转次序不影响最终结果. 有点像二分搜索时用的逐个试的方法,每次翻的 ...
- 挑战程序竞赛 反转开关 poj3276
这个我其实也没有看太懂它的证明过程. 1.若某一个位置被翻转了n次,则其实际上被翻转了n%2次. 2.分析易知翻转的顺序并不影响最终结果. 3.现在我们着眼于第1个位置,可知若要将第1个位置进行翻转只 ...
- Face The Right Way [POJ3276] [开关问题]
题意: 有n头奶牛排成一排,有的朝前(F)有的朝后(B),现在你可以使k头奶牛一次性翻转朝向(n>=k>=1),问你最少的翻转次数和此时对应的k值. Input Line 1: A sin ...
- POJ3276(遍历+区间修改)
http://poj.org/problem?id=3276 题意:n(n<=5000)头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全 ...
- poj3276 Face The Right Way
Face The Right Way POJ - 3276 题目大意: n头牛排成一列,每头牛向前或向后,为了让所有牛都面向前方,设定一个k值,每操作一次恰好使k头连续的牛转向,求最少的操作次数m和对 ...
- POJ3276 Face The Right Way 开关问题
①每个K从最左边进行考虑 ②f[i]=[i,i+k-1]是否进行反转:1代表是,0代表否 ③∑ (i)(i=i+1-K+1) f[j]=∑ (i-1)(i=i-K+1) f[j]+f[i]-f[i-K ...
- Face The Right Way 开关(POJ3276)
描述: \( N 头牛排成了一列.每头牛或者向前或者向后.为了让所有的牛都面向前方,农夫约翰买了 一台自动转向的机器. 这个机器在购买时就必须设定一个数值 K,机器每操作一次恰好使 K 头连续的牛转向 ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- The Water Bowls [POJ3185] [开关问题]
题意 一串长度为20的0,1数列,每次翻转i,会影响i-1,i+1,也被翻转,最少翻转成0的步骤数是多少? Sample Input 0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 ...
随机推荐
- Tomcat 安装与配置
1. 将压缩文件“apache-tomcat-7.0.62.zip ”上传到linux系统目录:/home/下 2. 进入目录 cd /home/ 解压文件,执行如下命令:unzip apache-t ...
- ios runtime 打印内 内部调用的属性
unsigned int count = 0; // 拷贝出所有的成员变量列表 Ivar *ivars = class_copyIvarList([UITextField class], &c ...
- 给sftp创建新用户、默认打开和限制在某个目录
一.环境: CentOS 6.8 使用 FileZilla 进行 sftp 连接 二.背景 给外包的工作人员提供我司服务器的某一目录的访问(包括读写)权限,方便他们部署代码文件. 之所以是某一目录的访 ...
- C/C++遍历目录下的所有文件(Windows篇,超详细)
注: 1. 本文讨论的是怎么用Windows API遍历目录下的所有文件.除Windows API,还有一种Windows/Linux通用的方式,使用<io.h>. 2. 本文部分翻译自M ...
- Tips_of_JS 之 利用JS实现水仙花数的寻找与实现斐波那契数列
一.水仙花数 1.啥是水仙花数? 水仙花数是指一个 n 位正整数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身.(例如:1^3 + 5^3+ 3^3 = 153) 2.利用JS实现对水 ...
- javaCV开发详解之2:推流器实现,推本地摄像头视频到流媒体服务器以及摄像头录制视频功能实现(基于javaCV-FFMPEG、javaCV-openCV)
javaCV系列文章: javacv开发详解之1:调用本机摄像头视频 javaCV开发详解之2:推流器实现,推本地摄像头视频到流媒体服务器以及摄像头录制视频功能实现(基于javaCV-FFMPEG.j ...
- 修复python的ModuleNotFoundError
我在项目里面用到了python,但其他的同事并没有安装python环境,为了不强制每个人都安装python,我下载了python-3.6.1-embed-amd64,并将用一个.bat去调用它. 大概 ...
- 导入网页数据到 Google Sheet
数据没有用,我们需要的是数据所反映出来的东西.增长率,排名,占比等.而这些结果是通过分析数据得到的. 从网上搜集到数据后,导入到表格程序中便可以进行方便地分析处理了.下面介绍将网页中的表格数据导入到 ...
- DB2简介和安装部署
一.DB2相关概念 1.DB2体系结构: DB2体系结构中的最高一层是系统,一个系统表示DB2的一个安装.在由很多机器组成 的网络环境中,我们有时=也称系统为数据库分区,一个系统可以包含多个DB2实例 ...
- SICP-2.2-数据的抽象
数据的抽象 生活中有许多的事物具有复合结构,例如地理位置所用的经纬度,便是通过一个复合结构来代表位置,在我们的程序当中,我们设法将经度纬度组合成一对,我们既可以把他们当做一个整体单元来进行操作,而且也 ...