POJ 3276 Face The Right Way (尺取法)
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
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 就需要改变 改变i到i+k-1的位置
一直找到n-k+1处 之后判断从n-k+2到n是否满足了题意 注意不要真的去修改
用一个sum值标记前面k-1个修改了多少次用来决定当前的是否要修改 利用尺取法 即前后推进
代码:
#include<stdio.h>
#include<string.h>
int a[5100],n,flag[5100];
int solve(int k)
{
int i;
memset(flag,0,sizeof(flag));//flag[i]表示区间[i,i+k-1] 是否需要翻转
int sum=0,cnt=0;//前k-1个转变的次数
for(i=1; i<=n-k+1; i++) //sum记录走到当前i,其前面k-1个翻转了多少次
{
if(i-k>=1)///因为每次翻转的是k个,
{
sum-=flag[i-k];
}
if(a[i]==0&&sum%2==0)///如果是B 且前面翻转了偶数次 仍旧需要翻转
{
flag[i]=1;
sum+=flag[i];
cnt++;
}
else if(a[i]==1&&sum%2==1)///如果是F 且前面翻转了奇数次
{
flag[i]=1;
sum+=flag[i];
cnt++;
}
// printf("i=%d flag[i]=%d\n",i,flag[i]);
}
for(i; i<=n; i++)
{
if(i-k>=1)
{
sum-=flag[i-k];
}
///这就相当于还没有翻转好
if(sum%2==0&&a[i]==0) return -1;
else if(sum%2==1&&a[i]==1) return -1;
}
return cnt;
}
int main()
{
int i,k,mn;
char s[2];
while(scanf("%d",&n)!=EOF)
{
mn=100010000;
for(i=1; i<=n; i++)
{
scanf("%s",s);
if(s[0]=='B') a[i]=0;
else if(s[0]=='F') a[i]=1;
}
// for(int i=1;i<=n;i++)
// printf("%d",a[i]);
// printf("\n");
k=1;
for(i=1; i<=n; i++)///i是指对应的间距
{
int mid=solve(i);///mid是指在相应的间距下的操作次数
///printf("k=%d,cnt=%d\n",i,mid);
if(mid==-1) continue;///在这个间距k下不能满足情况
if(mn>mid)
{
mn=mid;
k=i;
}
}
printf("%d %d\n",k,mn);
}
return 0;
}
POJ 3276 Face The Right Way (尺取法)的更多相关文章
- POJ 3061 (二分+前缀和or尺取法)
题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...
- POJ 3320 Jessica's Reading Problem 尺取法
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
- poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用
jessica's Reading PJroblem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9134 Accep ...
- poj 2566Bound Found(前缀和,尺取法)
http://poj.org/problem?id=2566: Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissi ...
- POJ 3320 Jessica's Reading Problem 尺取法/map
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7467 Accept ...
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- 尺取法 poj 2566
尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...
- POJ 3320 尺取法(基础题)
Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...
随机推荐
- lintcode-153-数字组合 II
153-数字组合 II 给出一组候选数字(C)和目标数字(T),找出C中所有的组合,使组合中数字的和为T.C中每个数字在每个组合中只能使用一次. 注意事项 所有的数字(包括目标数字)均为正整数. 元素 ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺-3
一.当天站立式会议照片: 二.项目进展 昨天已完成的工作: 依靠HTML 逻辑框架等技术完成程序界面前端的实现. 明天计划完成的工作: 依靠css 逻辑框架等技术完成程序界面前端的实现. 工作中遇到的 ...
- 结对作业二——WordCount进阶版
软工作业三 要求地址 作业要求地址 结对码云项目地址 结对伙伴:秦玉 博客地址 PSP表格 PSP2.1 个人开发流程 预估耗费时间(分钟) 实际耗费时间(分钟) Planning 计划 10 7 · ...
- Android基础------SQLite数据库(二)
1.操作SQLite数据库 1.1 execSQL() 可以执行insert.delete.update和CREATE TABLE之类有更改行为的SQL语句 1.2 rawQuery() 可以执行se ...
- P3808 【模板】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...
- [洛谷P3931]SAC E#1 - 一道难题 Tree
题目大意:给你一棵带权有根树,可以切断一些边,问使得根和叶子节点不连通的最小代价. 题解:做了一天的网络流,这道题显然可以用最小割来做,但是也可以用树形$DP$,基本同[SDOI2011]消耗战,这道 ...
- UOJ228:基础数据结构练习题——题解
http://uoj.ac/problem/228 参考:https://www.cnblogs.com/ljh2000-jump/p/6357583.html 考虑当整个区间的最大值开方==最小值开 ...
- HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化)
HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化) 题意分析 首先C表示测试数据的组数,然后给出经费的金额和大米的种类.接着是每袋大米的 ...
- Mysql Fabric实现学习笔记
Mysql Fabric用来管理mysql服务,提供扩展性和容易使用的系统,管理mysql分片和高可用部署(当前实现了两个特性:高可用和使用数据分片的横向扩展,能单独使用或结合使用这两个特性.). 架 ...
- Navcat中Oracle连接的坑-Instant Client
报错信息: 官方下载Instant Client下载: http://www.oracle.com/technetwork/cn/topics/intel-macsoft-102027-zhs.htm ...