[プログラミング練習]POJ 3276 Face The Right Way

2988 ワード

POJ 3276 Face The Right Way
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • 構想
  • コード
  • 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
    構想
  • 反転(スイッチ)という問題で、比較的暴力的な解法は、最初の牛から順にその向きを判断し、反転すると、その牛からの連続K牛を反転(K=1,2,3...N)、時間複雑度O(N 3)、Nを5000ととるとタイムアウトとなる.
  • 改良:状態記録配列f[n+1]、f[i]は、i番目の牛とi-1番目の牛の向きの相対位置の値(i=0の場合、初期状態「F」とする)を記録し、向きが同じであればf[i]は1をとり、逆に0をとる.反転する度に、実際には第i牛と第i+k牛の相対位置だけが変化し、中間部分の牛の相対状態は何も変化せず、第i牛と第i+k牛の状態をリアルタイムで更新した後、残りの牛が正しい方向に向いているかどうかを確認し(現在のkが可能かどうかを検証)、最終的に最小反転回数を求めることができる.この方法は時間複雑度O(N 2)である.

  • コード#コード#
    #include 
    #include 
    
    using namespace std;
    const int max_n = 5005;
    int f[max_n], dir[max_n], n, K, M;
    
    int calc(int k){
        int res = 0;
        for(int i=1; i<=n-k+1; ++i){
            if(f[i]){
                ++res;
                f[i+k]^=1;
            }
        }
        for(int i=n-k+2; i<=n; ++i){
            if(f[i]){
                res = -1;
                break;
            }
        }
        return res;
    }
    
    int main()
    {
        cin >> n;
        if(n<0) return 0;
    
        char s, base = 'F';
        for(int i=1; i<=n; ++i){
            cin >> s;
            if(s!=base)	dir[i] = 1;
            base = s;
        }
    
        M = n;
    
        for(int k=1; k<=n; ++k){
            memcpy(f, dir, sizeof(dir));
            int m = calc(k);
            if(m>=0 && M>m){
                K = k;
                M = m;
            }
        }
        cout << K << ' ' << M << endl;
        return 0;
    }