zoukankan      html  css  js  c++  java
  • 【反转开灯问题】Face The Right Way

    题目

    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头牛,每个牛有自己的方向,B是背对,F是正向。你可以选择连续的K头牛转向,如何使用一个K使得操作数最少,K尽量小。

    思路

    从开头开始搜索,如果为B的话就要改变i到i+k-1的地方。一直到n-k+1。然后判断n-k+2到n跟题意是否一致。(PS:不要真的去修改,只是模拟)

    然后sum来记录前k-1个修改几次再决定是不是真的要修改(利用尺取法)。
    (PS:尺取法(我在看其他题解时看到的,了解一下):顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案。尺取法比直接暴力枚举区间效率高很多,尤其是数据量大的时候,所以说尺取法是一种高效的枚举区间的方法,是一种技巧,一般用于求取有一定限制的区间个数或最短的区间等等。当然任何技巧都存在其不足的地方,有些情况下尺取法不可行,无法得出正确答案,所以要先判断是否可以使用尺取法再进行计算。)

    下面看一下代码(理解理解)

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    int a[5100],n,flag[5100];
    int solve(int k){
        int i;
        memset(flag,0,sizeof(flag));//flag[i]表示区间[i,i+k-1] 是否需要翻转
        int sum=0,cnt=0;//前k-1个转变的次数
        for(i=1;i<=n-k+1;i++){//sum记录走到当前i,其前面k-1个翻转了多少次
            if(i-k>=1){
                sum-=flag[i-k];
            }
            if(a[i]==0&&sum%2==0){//如果是B 且前面翻转了偶数次 仍旧需要翻转
                 flag[i]=1;
                 sum+=flag[i];
                 cnt++;
            }
            else if(a[i]==1&&sum%2==1){//如果是F  且前面翻转了奇数次
                flag[i]=1;
                sum+=flag[i];
                cnt++;
            }
     
        }
     
            for(i;i<=n;i++)
            {
               if(i-k>=1)
               {
                  sum-=flag[i-k];
               }
               if(sum%2==0&&a[i]==0) return -1;
               else if(sum%2==1&&a[i]==1) return -1;
            }
            return cnt;
     
     
    }
    int main()
    {
        int i,k,mn;
        char s[2];
        while(scanf("%d",&n)!=EOF)
        {
            mn=100010000;
            for(i=1;i<=n;i++)
            {
                scanf("%s",s);
                if(s[0]=='B') a[i]=0;
                else if(s[0]=='F') a[i]=1;
     
            }
            k=1;
            for(i=1;i<=n;i++)
                {
                    int mid=solve(i);
                    //printf("k=%d,cnt=%d
    ",i,mid);
                    if(mid==-1) continue;
                    if(mn>mid) {mn=mid;k=i;}
                }
            printf("%d %d
    ",k,mn);
        }
        return 0;
    }
  • 相关阅读:
    ES6 数值
    ES6 字符串
    ES6 Reflect 与 Proxy
    ES6 Map 与 Set
    es6 Symbol
    新兴的API(fileReader、geolocation、web计时、web worker)
    浏览器数据库 IndexedDB 入门教程
    离线应用与客户端存储(cookie storage indexedDB)
    javascript高级技巧篇(作用域安全、防篡改、惰性载入、节流、自定义事件,拖放)
    ajax与comet
  • 原文地址:https://www.cnblogs.com/Vocanda/p/12642848.html
Copyright © 2011-2022 走看看