1. 程式人生 > >依賴型的關系建立 - 小技巧

依賴型的關系建立 - 小技巧

for art reat 復雜度 ger vector cnblogs same const

依賴性的關系 : 就是指其後一個的狀態由其前一個所決定,這種優化方法可以用在很多的地方,

例如 : 一串東西,有正有反,每次只能操作一段區間,將此區間正反顛倒,向這種模型就可以建立成依賴型的模型,改變一個區間的【a, b】的時候,實則我只要改變 a 的依賴值與 b+1 的依賴值。那麽對於整個區間便進行了操作,復雜度為 0(1)。

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 ≤ KN)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) 題意 : B 表示朝向背面 , F 表示朝向正面,問要經歷最小的操作次數,使所有的面是正面朝上。 並輸出此時對應的更改區間K 值。 思路 : 枚舉所有可能的更改區間的 k 值,每次更改時由左向右更改,當第一個更改完後,便不會再有任何更改可以影響到他。 代碼示例 :
/*
 * Author:  ry 
 * Created Time:  2017/10/30 16:18:08
 * File Name: 1.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
const int eps = 1e6+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long

int n;
int pre[5005];
int arr[5005];
int main() {
    cin >> n;
    char ch, last = ‘F‘;
    
    getchar();
    int k = 1;
    for(int i = 0; i < n; i++){
        scanf("%c", &ch);
        getchar();
        if (ch != last){
            pre[k++] = 1;
        }
        else pre[k++] = 0;
        last = ch;
    }
    //for(int i = 0; i < k; i++){
        //printf("%d\t", pre[i]);
    //}
    int ans = 1<<30, ans2 = 0;
    for(int i = 1; i <= n; i++){
        memcpy(arr, pre, sizeof(pre)); 
        int temp = 0;
        for(int j = 1; j <= n-i+1; j++){
            if (arr[j]){
                temp++;
                arr[j+i] ^= 1;
            }
        } 
        for(int j = n-i+2; j <= n; j++){
            if (arr[j]){
                temp = 1 << 30;
                break;
            }
        }
        if (temp < ans){
            ans = temp;
            ans2 = i;
        }
    }
    printf("%d %d\n", ans2, ans);
    return 0;
}

依賴型的關系建立 - 小技巧