码迷,mamicode.com
首页 > 其他好文 > 详细

依赖型的关系建立 - 小技巧

时间:2017-10-30 18:24:26      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:tor   stand   character   cpp   ace   turn   any   hint   ast   

依赖性的关系 : 就是指其后一个的状态由其前一个所决定,这种优化方法可以用在很多的地方,

例如 : 一串东西,有正有反,每次只能操作一段区间,将此区间正反颠倒,向这种模型就可以建立成依赖型的模型,改变一个区间的【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;
}

 

依赖型的关系建立 - 小技巧

标签:tor   stand   character   cpp   ace   turn   any   hint   ast   

原文地址:http://www.cnblogs.com/ccut-ry/p/7755335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!