POJ_3276_Face_The_Right_Way_(反转)

描述


http://poj.org/problem?id=3276

n头牛排成了一列.每头牛向前或者向后.要让所有牛的面向前方,现在每次要转k头连续的牛,求最少反转次数m与此时的k.

 

Face The Right Way
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3899   Accepted: 1799

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 ≤ KN)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

分析


反转问题.

对于这样的连续区间的开关问题,有两个特点:

1.区间开关的顺序无所谓.

2.区间开关奇数次相当于开关1次,开关偶数次相当于不开关,所以只存在开,不开两个状态.

用false表示正确的情况与不开关,true表示不正确的情况与开关,然后使用^(亦或)进行计算. 枚举k的值,对于最左边的点,影响它的只有最左边的那一个区间,如果这个点需要改变,最左边的区间就改变,反之亦然.进行过这个区间的操作后,最左边的点就确定下来了,可以扔掉,这样第二个点就成了最左边的点,继续进行操作即可.到最后总长度小于k时,无法再改变区间了,就扫一遍判断是否都满足正确的情况即可.

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 
 5 const int maxn=5005;
 6 int n;
 7 bool a[maxn],f[maxn];
 8 
 9 int J(int k)
10 {
11     int res=0;
12     bool sum=false;
13     memset(f,false,sizeof(f));
14     for(int i=1;i+k-1<=n;i++)
15     {
16         if(sum^a[i]) 
17         {
18             f[i]=true;
19             res++;
20         }
21         sum^=f[i];
22         if(i-k+1>0) sum^=f[i-k+1];
23     }
24     for(int i=n-k+2;i<=n;i++)
25     {
26         if(sum^a[i]) return -1;
27         if(i-k+1>0) sum^=f[i-k+1];
28     }
29     return res;
30 }
31 
32 void solve()
33 {
34     int times=n+1,k;
35     for(int i=1;i<=n;i++)
36     {
37         int ans=J(i);
38         if(ans<times&&ans>=0) 
39         {
40             times=ans;
41             k=i;
42 
43         }
44     }
45     printf("%d %d\n",k,times);
46 }
47 
48 void init()
49 {
50     scanf("%d\n",&n);
51     for(int i=1;i<=n;i++)
52     {
53         char c;
54         scanf("%c\n",&c);
55         if(c=='B') a[i]=true;
56         else a[i]=false;
57     }
58 }
59 
60 int main()
61 {
62     freopen("face.in","r",stdin);
63     freopen("face.out","w",stdout);
64     init();
65     solve();
66     fclose(stdin);
67     fclose(stdout);
68     return 0;
69 }
View Code

 

posted @ 2016-04-22 23:31  晴歌。  阅读(206)  评论(0编辑  收藏  举报