• POJ3276 Face The Right Way(开关问题)


    题目链接

    Face The Right Way

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6823   Accepted: 3166

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

    N头牛排成了一列。每头牛头向前或向后。为了让所有的牛都面向前方,农夫约翰买了一台自动转向的机器。这个机器在购买时就必须设定一个数值K,机器每操作一次恰好使K头连续的牛转向。求让所有牛都能面向前方需要的最少操作次数M和对应的最小的K.

    1、交换区间反转的顺序对结果是没有影响的。

    2、对同一个区间进行两次以上的反转是多余的。

    考虑最左端的牛,包含这头牛的区间只有一个,如果这头牛面朝前方,则这个区间不需要反转;如果这头牛面朝后方,则这个区间需要反转,而且在此之后这个最左的区间就不需要再考虑了。通过首先考虑最左端的牛,问题的规模就缩小了1。如此重复下去每次判断最左端的牛。

    令f[i]=1表示第i,i+1,i+2……,i+k-1头牛即第i头牛为最左端所在的区间需要反转。

    sum=sum_{j=i-k+1}^{i-1}f[j],表示第i头牛前反转的区间对第i头牛的影响,若sum为奇数,则第i头牛与最开始的朝向相反,若sum为偶数,则第i头牛与最开始的朝向相同。

    sum_{j=i-k+2}^{i}f[j]=sum_{j=i-k+1}^{i-1}f[j]+f[i]-f[i-k+1]

    AC代码:

     1 #include<iostream>
     2 #include<sstream>
     3 #include<algorithm>
     4 #include<string>
     5 #include<cstring>
     6 #include<iomanip>
     7 #include<vector>
     8 #include<cmath>
     9 #include<ctime>
    10 #include<stack>
    11 #include<queue>
    12 #define e 2.71828182
    13 #define Pi 3.141592654
    14 using namespace std;
    15 int N,f[5010];//f[i]为1表示第i,i+1,i+2,,,i+k-1头牛都反转 
    16 char cow[5010];
    17 int fun(int k)
    18 {
    19     memset(f,0,sizeof(f));
    20     int ans=0;//反转的次数 
    21     int sum=0;
    22     //第i头牛是否需要翻转与它本身的状态和f[i-k+1],f[i-k+2],,,f[i-1]有关 
    23     for(int i=1;i<=N-k+1;i++)
    24     {
    25         if(sum%2==0&&cow[i]=='B')//前面的翻转并没有改变第i头牛的转向 
    26             f[i]=1,ans++; 
    27         else if(sum%2==1&&cow[i]=='F')//前面的翻转改变了第i头牛的转向 
    28             f[i]=1,ans++;
    29         
    30         sum+=f[i];
    31         if(i-k+1>=1) sum-=f[i-k+1];
    32     }
    33     //第N-k+2头牛之后不能再反转了,判断剩下的牛朝向是否满足条件 
    34     for(int i=N-k+2;i<=N;i++)
    35     {
    36         if(sum%2==0&&cow[i]=='B') return -1;//不满足则返回-1 
    37         else if(sum%2==1&&cow[i]=='F') return -1;
    38         
    39         sum+=f[i];
    40         if(i-k+1>=1) sum-=f[i-k+1];
    41     }
    42     return ans;
    43 }
    44 int main()
    45 {
    46     cin>>N;
    47     for(int i=1;i<=N;i++) cin>>cow[i];
    48     
    49     int k=1<<30,m=1<<30;
    50     for(int i=1;i<=N;i++)//一次使连续的i头牛转向 
    51     {
    52         if(fun(i)>=0&&fun(i)<m)
    53         k=i,m=fun(i);
    54     }
    55     cout<<k<<' '<<m;
    56 }
    View Code
  • 相关阅读:
    mysql 用sql语句查询一个表中的所有字段类型、注释
    一个数的因子是什么?如何求一个数的所有因子?
    anaconda安装opencv方法
    Batch Normalization和Instance Normalization
    Windows下无法下载torchvision的解决方法
    atoi函数--把参数 str 所指向的字符串转换为一个整数(类型为 int 型)
    特殊处理---清华机试(string类型转换成int类型)
    malloc和new
    C语言字符串输入总结整理
    二分查找
  • 原文地址:https://www.cnblogs.com/wangzhebufangqi/p/12796126.html
Copyright © 2020-2023  润新知