Face The Right Way
| Time Limit:2000 MS |
|
Memory Limit:65536 K |
| Total Submissions:2721 |
|
Accepted:1246 |
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
7 BBFBFBB
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
/*** Problem: POJ No. 3276 *** Running time: 297 MS *** Complier: C ++ *** Author: Changmu: A column of 1 <= N <= 5000. Each ox can either forward or backward. In order to make all the cows face the front, the farmer can turn K heads of cattle to 1 <= K <= N, calculate the minimum ** number of operations M and the corresponding minimum K. * ** Solution: as the switching interval flip sequence does not affect the result, the cattle needing to be flipped are reversed from left to right, at the same time, record the impact on other cattle in this range, that is, sum in cal. ** check whether there is a reverse bull in the last segment that cannot be flipped. If yes, the solution fails. */# Include <stdio. h> # include <string. h> # define maxn 5010int cow [maxn], N, K, M; // The initial position of the cows, int tra [maxn] after 0 and 1; // whether to flip the I header, 1 flip int cal (int k) {// The Flip order from left to right int ans = 0, I, sum = 0; for (I = 0; I + k <= N; ++ I) {tra [I] = 0; // initialize if (sum + cow [I]) & 1) {tra [I] = 1; ++ ans;} sum + = tra [I]; // if (I-k + 1> = 0) sum-= tra [I-k + 1];} // Check whether there are reverse cows for (; I <N; ++ I) if (sum + cow [I]) & 1) return-1; else if (I-k + 1> = 0) sum-= tra [I-k + 1]; return ans;} int main () {char ch [2]; int I, m, k; while (scanf ("% d", & N) = 1) {for (I = 0; I <N; ++ I) {scanf ("% s ", ch); if (ch [0] = 'B') cow [I] = 1; else cow [I] = 0;} M = N; K = 1; for (k = 1; k <= N; ++ k) {m = cal (k); if (m> = 0 & m <M) {M = m; K = k ;}} printf ("% d \ n", K, M);} return 0 ;}
POJ3276 Face The Right Way [ruler acquisition method]