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

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

转载于:https://www.cnblogs.com/ccut-ry/p/7755335.html

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

  1. Word小技巧:秒做组织架构图,让你一目了然各部门相互关系

    今天我们来分享一下word怎么做组织架构图,那么,什么是组织架构图呢?它是表现雇员.职称和群体关系的一种图表,形象的反映了组织内各机构.岗位和上下左右相互之间的关系.一个架构图就能一目了然各个部门之间 ...

  2. iOS小技巧12-苹果地图和高德地图的关系

    iOS小技巧12-苹果地图和高德地图的关系 苹果地图是美国苹果公司(Apple Inc.)研发的地图产品,运行于iOS系统. 高德地图是中国高德(AutoNavi)研发的地图产品,有iOS.Andro ...

  3. python查询模块所有类_python 小技巧(import模块、查询类继承关系、安装包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在这里列举一些我使用Python时积累的小技巧.这些技巧是我在使用Python过程 ...

  4. 鸟哥的Linux私房菜(基础篇)-第四章、安装 CentOS 5.x 与多重开机小技巧

    第四章.安装 CentOS 5.x 与多重开机小技巧 最近更新日期:2009/08/11 Linux distributions越作越成熟,所以在安装方面也越来越简单!虽然安装非常的简单,但是刚刚前一 ...

  5. 电脑没有ps怎么改照片dpi_PS入门的小技巧来啦!小白们还在等什么呢?快进来啊!...

    哈啰!大家下午好!今天不归给大家带来的不是教程啦,而是不归自己对于PS这门软件的一些心得体会和一些小技巧,希望这点小技巧对于大家工作和学习上能有一点帮助. 像素和分辨率 1. 像素是构成图像的最小单位 ...

  6. 【Xamarin挖墙脚系列:代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧(转)】...

    正愁如何选择构建项目中的视图呢,现在官方推荐画板 Storybord...但是好像 xib貌似更胜一筹.以前的老棒子总喜欢装吊,用代码写....用代码堆一个HTML页面不知道你们尝试过没有.等页面做出 ...

  7. 师妹问我:有哪些新手程序员不知道的小技巧?

    阅读本文大概需要5分钟. 一个师妹问:洋哥,我今年应届毕业,刚开始写代码,不知道有没有一些新手需要注意的地方. 给了师妹一些建议之后,感觉这是个好问题!不光是新手程序员,很多小技巧小秘密恐怕老手也未必 ...

  8. 【MySQL】37 个 MySQL 数据库小技巧

    1.概述 转载:37 个 MySQL 数据库小技巧,不看别后悔! 无论是运维.开发.测试,还是架构师,数据库技术是一个必备加薪神器,那么,一直说学习数据库.学MySQL,到底是要学习它的哪些东西呢? ...

  9. 38个MySQL数据库的小技巧

    1.如何快速掌握MySQL? 培养兴趣 兴趣是最好的老师,不论学习什么知识,兴趣都可以极大地提高学习效率.当然学习MySQL 5.6也不例外. 夯实基础 计算机领域的技术非常强调基础,刚开始学习可能还 ...

最新文章

  1. Linux日常命令记录
  2. python 调用sqldr_sqlldr并发
  3. 实体entity、JavaBean、Model、POJO、domain的区别
  4. CocoaPods pod install/pod update更新慢的问题
  5. Java网络编程从入门到精通(5):使用InetAddress类的getHostName方法获得域名
  6. 【华为云技术分享】MongoDB经典故障系列三:副本集延迟太高怎么办?
  7. css3切角文本框_CSS3:linear-gradient切角画册
  8. java 如何秒速写一个线程启动
  9. Hibernate复习笔记
  10. 帆软自定义登录html,自定义登陆界面- FineReport帮助文档|报表开发|报表使用|学习教程...
  11. 基于C语言及51单片机的PID控制电机调速详解(附详细代码及Protsus仿真)
  12. 旁路电容0.1uF,是怎么来的?
  13. 微信小程序图片宽高自适应
  14. 基于alphalens对qlib的alpha158做单因子分析
  15. win10无限蓝屏_windows10系统蓝屏无限重启命令解决教程
  16. 高性能消息中间件 nsq 解析-介绍
  17. 根据示波器存储的波形数据得到两列信号的相位差(MATLAB源码)
  18. 「表白神器」Python超火隐藏表白图 你能看出来吗?「附源码」
  19. 冯诺依曼体系各硬件工作原理解析
  20. mib browser打开mib文件

热门文章

  1. 网络技术独立的×××-SDN和三个问题
  2. “无继承”情况下的对象构造
  3. Linux下双网卡绑定技术实现负载均衡和失效保护 bond
  4. mac terminal ssh client shuttle 免输密码
  5. ABI (应用程序二进制接口)
  6. 我的软件项目过程管理经验
  7. DEDECMS系统后台添加菜单列表
  8. javascript 滚动+停留 代码
  9. 【Solidity】1.一个Solidity源文件的布局 - 深入理解Solidity 1
  10. 从SQL Server数据库转到Oracle数据库的数据脚本处理