Poj 3276 face the right way

Source: Internet
Author: User

Face the right way
Time limit:2000 ms   Memory limit:65536 K
Total submissions:2665   Accepted:1233

Description

Farmer John has arranged hisN(1 ≤N≤ 5,000) cows in a row and rows 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 turnK(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 ction of a contiguous groupKCows in the line (one cannot use it on fewerKCows, e.g., at the either end of the line of cows). Each cow remains in the same* Location *As before, but ends up facing* 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 valueK, Please help him determine the minimum valueKThat minimizes the number of operations required by the machine to make all the cows face forward. Also determineM, The minimum number of machine operations required to get all the cows facing forward using that valueK.

Input

Line 1: A single INTEGER: N 
Lines 2 .. N+ 1: Line I+ 1 contains a single character, FOr B, Indicating whether cow IIs facing forward or backward.

Output

Line 1: two space-separated integers: KAnd 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)

Algorithm analysis:

The general question is that there is a nheaded ox. in a straight line, there are different directions. Before and after, let you look for a K value, so that each time you can only reverse K cows, minimum number of answers and minimum K.

When I read this question for the first time, I think it is still a bit tricky, because if I want to change the direction of a cow, it will definitely affect K cows, but I am thinking about it, when a cow is reversed multiple times of 2, and the initial direction is the same, can be considered as no reversal, because to enumerate K and N-K + 1 (worst case) reverse, in addition, the value of the Dir (Direction array) array must be constantly changed for each flip, so the complexity O (N ^ 3), timeout, however, we can consider using a new array f [I] to solve this problem. F [I] (0 or 1) is used to store the reversal of each position, reducing the complexity to O (N ^ 2) based on the following code:


# Include <iostream> using namespace STD; # define maxn 5000int dir [maxn]; // cattle direction, 0 F 1 bint f [maxn]; // record whether each position is reversed. If it is an odd number, it can be considered that int N is not reversed. Int calc (int K) {int I, sum = 0, Res = 0; // use sum to record the number of reverse operations that have an impact on "current" f [I], and res to record the total number of reverse operations memset (F, 0, sizeof (f )); for (I = 0; I + k <= N; I ++) // pay attention to the ending condition I> n-k, that is, I = n-k + 1 {If (dir [I] + sum) % 2! = 0) {res ++; F [I] = 1;} sum + = f [I]; // The sum value comes from the number of times the record is reversed, if the current I exceeds the k range, // The I + 1-k and its previous position will not affect the number of reverse operations of the current I, so if (I + 1> = K) is subtracted) sum-= f [I + 1-k];} for (I = n-k + 1; I <n; I ++) {If (dir [I] + sum) % 2! = 0) Return-1; if (I + 1> = k) sum-= f [I + 1-k];} return res;} void solve () {int P, q, ANS = n + 1; for (INT I = 1; I <= N; I ++) {P = calc (I); If (P! =-1) {ans = (ANS <p? Ans: P); q = I ;}} cout <q <"" <ans <Endl ;}int main () {CIN> N; char C; int I; for (I = 0; I <n; I ++) {CIN> C; dir [I] = (C = 'F '? ); Getchar ();} solve (); Return 0 ;}


Poj 3276 face the right way

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.