poj3276

Face The Right Way

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

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

7
B
B
F
B
F
B
B

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)

Source

USACO 2007 March Gold

 

 

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;
		}
	}

}

  

时间: 2024-10-05 20:30:45

poj3276的相关文章

【反转】POJ3276

Face The Right Way 题意:N个牛 每个都有一定的方向 B背对 F表示头对着你 给你一个装置 每次可以选择连续的K个牛反转方向 问你如何选择K 使得操作数最少 k也应尽量小. 例子:   N=7   BBFBFBB   (F:前面   B:后面)    (红色的为要反转的)   此出K=3  M=3 B B F B F B B F F B B F B B F F F F B B B F F F F F F F   成功了 如果用暴力的方法的话:   考虑N头牛的话最坏情况下要进行

poj3276 Face The Right Way(反转问题,好题)

https://vjudge.net/problem/POJ-3276 首先意识到,对一个区间进行两次及以上的反转是没有意义的,而且反转次序不影响最终结果. 有点像二分搜索时用的逐个试的方法,每次翻的个数从1~n,然后进入函数判断. 由于正反性可以很巧妙地利用计数的奇偶来判断,所有这里优化复杂度,用f[i]记录i~i+k-1是否翻转了,不断向右判断,如果是反面就反转接下来的一组,直至最后,最后剩的几个如果全正就说明可以,如果有反面就说明不行. 1 #include<iostream> 2 #i

挑战程序竞赛 反转开关 poj3276

这个我其实也没有看太懂它的证明过程. 1.若某一个位置被翻转了n次,则其实际上被翻转了n%2次. 2.分析易知翻转的顺序并不影响最终结果. 3.现在我们着眼于第1个位置,可知若要将第1个位置进行翻转只有翻转它自己,因为没有其他位置的翻转会引起它的翻转. 由①可知若第1个位置为1则必须且进行翻转(并将其后2个进行连带翻转)且以后不再进行翻转,因为再进行翻转就一共翻转了2次相当于没翻转. 然后着眼于第2个位置,由于第1个位置不再进行翻转,所以要想翻转第2个位置只有翻转它自己,因为没有其他位置的翻转会

poj3276 Face The Right Way

Face The Right Way POJ - 3276 题目大意: n头牛排成一列,每头牛向前或向后,为了让所有牛都面向前方,设定一个k值,每操作一次恰好使k头连续的牛转向,求最少的操作次数m和对应的最小的k (B向后:F向前:输出为k m) Sample Input 7 B B F B F B B Sample Output 3 3 /* 枚举k值,对于枚举到的每个k值,求其m 对于一个长为k的区间的反转,不需要挨个模拟,设一个数组f[] f[i]记录第i个位置上的牛被反转了多少次 在考虑

POJ3276 Face The Right Way 【尺取法】

Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2721   Accepted: 1246 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 facin

POJ3276——暴力——Fliptile

http://poj.org/problem?id=3279 /* 枚举第一行所有的状态,接下来的行都已经确定了,字典序最小输出 */ /************************************************ * Author :Powatr * Created Time :2015-8-9 13:18:38 * File Name :D.cpp ************************************************/ #include <cs

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+1] 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<queue> 5 #include<map> 6 #include<vector>

POJ3276 Face The Right Way (尺取法)

题目描述 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

反转 开关问题

首先考虑最左端的牛.包含这头牛的区间只有一个,因此如果这头牛面朝前方,这个区间不反转,面朝后方则反转.以此类推,逐渐缩小问题规模. 用数组j[i]=1代表区间[i,i+K-1]进行了反转  j[i]=0代表不反转. 如果一头牛之前被反转的次数为奇数,则朝向和刚开始相反,为偶数则相同. #include<iostream> #include<memory.h> using namespace std; int N,dir[5005]; int j[5005]; //标记区间[i,i-