Face The Right Way
Time Limit: 2000 MS Memory Limit: 65536 K
Total Submissions: 1833 Accepted: 860
Description
Farmer John has arranged his N (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 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 ction 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
BSample 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
Question:
N cows each have a certain direction B back to F indicates the head to you give you a device each time you can choose K consecutive cattle reverse direction ask you how to choose K to make the operand at least k also should be as small as possible
// Train of thought: from the first to find if the current is B, you need to change the I to I + k-1 position // always find n-k + 1 and then judge from n-k + 2 to n whether the meaning of the question is satisfied do not really modify // use a sum value to mark the previous K-1 Modified how many times to determine whether to modify the current use of the ruler acquisition method that is before and after the propulsion # include <stdio. h> # include <string. h> int a [5100], n, flag [5100]; int solve (int k) {int I; memset (flag, 0, sizeof (flag )); // flag [I] indicates the range [I, I + k-1] whether to flip int sum = 0, cnt = 0; // The number of changes in the previous K-1 for (I = 1; I <= n-k + 1; I ++) // sum record goes to the current I, how many times have the previous K-1 been flipped {if (I-k> = 1) {sum-= flag [I-k];} if (a [I] = 0 & sum % 2 = 0) // If it is B and the previous number of times is flipped even, you still need to flip {flag [I] = 1; sum + = flag [I]; cnt ++ ;} else if (a [I] = 1 & sum % 2 = 1) // if it is F and the previous number of times is flipped {flag [I] = 1; sum + = flag [I]; cnt ++;} // printf ("I = % d flag [I] = % d \ n", I, flag [I]) ;}for (I; I <= n; I ++) {if (I-k> = 1) {sum-= flag [I-k];} if (sum % 2 = 0 & a [I] = 0) return-1; else if (sum % 2 = 1 & a [I] = 1) return-1;} return cnt ;}int main () {int I, k, mn; char s [2]; while (scanf ("% d", & n )! = EOF) {mn = 100010000; for (I = 1; I <= n; I ++) {scanf ("% s", s ); if (s [0] = 'B') a [I] = 0; else if (s [0] = 'F') a [I] = 1 ;} k = 1; for (I = 1; I <= n; I ++) {int mid = solve (I); // printf ("k = % d, cnt = % d \ n ", I, mid); if (mid =-1) continue; if (mn> mid) {mn = mid; k = I ;}} printf ("% d \ n", k, mn);} return 0 ;}