• POJ


    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)
    思维题  
     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include<cstdio>
    11 #include<cmath>
    12 #include<cstring>
    13 #include <cstdio>
    14 #include <cstdlib>
    15 #include<stack>
    16 #include<vector>
    17 int a[5100];
    18 int b[5100];
    19 int n;
    20 int sum;
    21 int add(int s)
    22 {
    23     //cout<<s<<"________________"<<endl;
    24     int add=0;
    25     memset(b,0,sizeof(b));
    26     sum=0;
    27     int i;
    28     for(i=1;i+s-1<=n;i++)
    29     {
    30         if((a[i]+sum)%2==1)
    31         {
    32             add++;
    33             b[i]=1;
    34         }
    35         sum+=b[i];
    36         if(i-s+1>0)
    37             sum-=b[i-s+1];
    38         //cout<<i<<"_"<<b[i]<<"_"<<sum<<endl;
    39     }
    40     for(;i<=n;i++)
    41     {
    42         if((a[i]+sum)%2==1)
    43             return -1;
    44         if(i-s+1>0)
    45             sum-=b[i-s+1];
    46     }
    47     return add;
    48 }
    49 
    50 int main()
    51 {
    52     scanf("%d",&n);
    53     char q[10];
    54     getchar();
    55     for(int i=1;i<=n;i++)
    56     {
    57         q[0]=getchar();
    58         if(q[0]=='B')
    59             a[i]=1;
    60         else
    61             a[i]=0;
    62         if(i==n)
    63             break;
    64         getchar();
    65     }
    66     int min1=n;
    67     int t=1;
    68     for(int i =1;i<=n;i++)
    69     {
    70         int s=add(i);
    71         if(s!=-1&&s<min1)
    72         {
    73             t=i;
    74             min1=s;
    75         }
    76     }
    77     printf("%d %d",t,min1);
    78     return 0;
    79 }
    View Code
  • 相关阅读:
    sql中not exists的用法
    jsp中target="_blank"的用法
    jsp 运用 session 登录输出
    jsp留言板
    编写JSP 实现用户登录并判断用户或密码
    jsp打印九九乘法表
    jsp输出当前时间
    jsp输出金字塔
    jsp输出5的阶乘
    jdbc练习3
  • 原文地址:https://www.cnblogs.com/dulute/p/7966716.html
Copyright © 2020-2023  润新知