• POJ 3276 Face The Right Way(反转)


     
    Face The Right Way
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6038   Accepted: 2791

    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)

    Problem Description

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

    思路:

    首先交换区间反转的顺序对结果是没有影响的,此外,可以知道对同一个区间进行两次以上的反转是多余的。 
    我们枚举K,然后从左往右,遇到需要反转的就给它反转,然后标记一下区间【i, i+k-1】被反转过。核心点就是在于如何标记能实现O(1)的复杂度。 
    用一个f[i]数组,f[i]: = 区间[i, i+k-1]进行了反转的话则为1,否则为0。这样,在考虑第i头牛时,如果区间[i-k+1,i-1]f[i]的和为奇数的话,则这头牛的方向与起始方向是相反的

    题解:1、5000头牛不是小数目,再怎么也得要n^2的算法,其中,枚举k是需要的,这就有n了,只能想办法给出一个n在O(n)时间内求出最小次数了。

       2、对于给定的k,要想O(n)内把次数算出来,即只能扫一遍,一想到的必定是从前往后扫,遇到面朝后的就转头,但这一转牵扯太多,要改太多东西,k一大直接崩溃。

       3、对于每次扫描到的第i个点,都至多只能改一次才能保证效率,即只改变化的。将牛的朝向弄成依赖型,即后者依赖于前者,这样在一个区间内[a,b]翻转时,实际上[a+1,b]的依赖关系是没有改变的,改变的只有a,b+1。

       4、综上,设置一种关系表示每头牛与前一头牛的朝向,最简单的就是同向与反向的差异,不妨令同向为0,反向为1,为了使得最后都朝前,可以令一头虚拟牛(即0号牛)头朝前,然后第一头牛依赖于它。

       5、因此,每次检查时,只需要更改a和a+k位置的牛的依赖关系便可以解决了,最后在检查一下剩余的牛是否全是0就结束了。

    详见代码注释
     1 #include<cstdio>  
     2 #include<cstring>  
     3 #include<iostream>  
     4 #include<algorithm>  
     5 #define inf 0x3f3f3f3f
     6 using namespace std;
     7 int d[5005];
     8 bool f[5005];
     9 int sum = 0;
    10 int n;
    11 
    12 int solve(int k)
    13 {
    14     memset(f, 0, sizeof(f));
    15     int i;
    16     sum = 0;
    17     int res = 0;
    18     for (i = 1; i + k - 1 <= n; i++)
    19     {
    20         if (i - k >= 1) sum -= f[i - k];//只需统计i-k+1到i即可,这之间翻转会影响到i,i-k为起点翻转k个,只能影响到第i-1个
    21         if ((d[i] + sum) % 2 !=0)
    22         {//包含了两种情况,1,d[i]为奇1,被翻了偶数次,仍未1。2,d[i]为偶0,被翻了奇数次,为1了
    23             res++;
    24             f[i] = 1;//i为B,需要反转
    25         }
    26         sum += f[i];
    27     }
    28 
    29     for (i; i <= n; i++)//检查剩下的牛是否有朝后的情况,i初始为=n-k+2
    30     {
    31         if (i - k >= 1) sum -= f[i - k];
    32         if ((d[i] + sum) % 2!=0) return -1;//剩下的还有为1的,没办法翻了
    33     }
    34     return res;
    35 }
    36 
    37 int main()
    38 {
    39     cin >> n;
    40     char a;
    41     int i;
    42     int k;
    43     for (i = 1; i <= n; i++)
    44     {
    45         cin >> a;
    46         if (a == 'B') d[i] = 1;
    47         else d[i] = 0;
    48     }
    49     int M = inf;
    50     int K = inf;
    51     for (k = 1; k <= n; k++)//题目说从1开始
    52     {
    53         int m = solve(k);
    54         if (m != -1 && M > m)//更新
    55         {
    56             M = m;
    57             K = k;
    58         }
    59     }
    60     cout << K << " " << M << endl;
    61     return 0;
    62 }
  • 相关阅读:
    nginx 09-Nginx部署https
    nginx 08-Nginx的rewrite规则
    nginx 07-Nginx缓存服务
    nginx 06-Nginx代理服务
    LBP及纹理表达 转自http://blog.sina.com.cn/s/blog_ba9d7d9901018k4v.html
    双边滤波与引导滤波
    层次聚类,转自http://blog.sina.com.cn/s/blog_62f3c4ef01014uhe.html
    有用的matlab函数(不断加入)
    显著目标检测思路
    matlab曲线、图形绘制方法(不断更新)
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271181.html
Copyright © 2020-2023  润新知