博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276
阅读量:6660 次
发布时间:2019-06-25

本文共 2630 字,大约阅读时间需要 8 分钟。

Face The Right Way
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2899   Accepted: 1338

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 ≤ 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

7BBFBFBB

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)
 
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 using namespace std;15 const int INF=0x4fffffff;16 const int EXP=1e-6;17 const int MS=5005;18 19 int dir[MS];20 int flag[MS]; // [i->i+k-1] 这个区间是否翻转。21 int N;22 23 int calc(int k)24 {25 int sum=0,res=0;26 memset(flag,0,sizeof(flag));27 for(int i=0;i+k-1
=0)36 sum-=flag[i-k+1];37 }38 // N-k+1 --> N-1 这个区间的任意一个位置起无法翻转。39 // 需要检查这段区间是不是全部朝前。40 for(int i=N-k+1;i
=0)45 sum-=flag[i-k+1];46 }47 return res;48 }49 50 51 void solve()52 {53 int K=1,M=N;54 for(int k=1;k<=N;k++)55 {56 int cnt=calc(k);57 if(cnt>=0&&M>cnt)58 {59 M=cnt;60 K=k;61 }62 }63 printf("%d %d\n",K,M);64 }65 66 int main()67 {68 char s[5];69 scanf("%d",&N);70 for(int i=0;i

 

转载于:https://www.cnblogs.com/767355675hutaishi/p/4376328.html

你可能感兴趣的文章
[Express] Level 3: Reading from the URL
查看>>
中国民航首条“自助智能安检通道”在广州启用
查看>>
linux 编译安装nginx
查看>>
vim编辑器详解
查看>>
小程序判断是否授权
查看>>
SpringMVC的第一个入门案例
查看>>
CentOs7.3 编译安装 Nginx 1.9.9
查看>>
Java语言基础——学习笔记
查看>>
ASP.NET Core中地址栏传入数据会影响Controller向ViewModel赋值
查看>>
hdu 三部曲 1Minimum Cost 最小费用最大流EK算法
查看>>
在virtualbox下使用vm映像文件
查看>>
vue聊天室|h5+vue仿微信聊天界面|vue仿微信
查看>>
常用的CDN 链接 http://cdn.code.baidu.com/ http://www.bootcdn.cn/
查看>>
窗口过程 - Windows程序设计(SDK)006
查看>>
线性表4 - 数据结构和算法09
查看>>
Python 函数的参数定义
查看>>
Using DDL Triggers in SQL Server 2005 to Capture Schema Changes
查看>>
[2778]小明的花费预算 (二分查找)SDUT
查看>>
SSH无密码验证
查看>>
使用HTML制作网页
查看>>