ACM题目————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
Lines 2..
N+1: Line i+1 contains a single character,
F or B, indicating whether cow i is facing forward or backward.
Output
K and M
Sample Input
7
B
B
F
B
F
B
B
Sample Output
3 3
Hint
题目大概意思就是说:n头牛,两个方向,向后B,向前F,每次转K头牛,最少转cnt次,
最终使所有的牛向前。求最少的K以及对应的cnt。
#include <stdio.h>
#include <string.h> #define maxn 5010 int cow[maxn], N, K, M; // 奶牛们的初始位置,0前1后
int tra[maxn]; // 是否翻转第i头牛,1翻 int cal(int k) // 翻转顺序从左往右
{
int ans = 0, i, sum = 0;
for(i = 0; i + k <= N; ++i)
{
tra[i] = 0;
if((sum + cow[i]) & 1)
{
tra[i] = 1;
++ans;
}
sum += tra[i]; // 尺取法
if(i - k + 1 >= 0)
sum -= tra[i-k+1];
}
// 检查剩下的是否有反向奶牛
for( ; i < N; ++i)
if((sum + cow[i]) & 1)
return -1;
else if(i - k + 1 >= 0)
sum -= tra[i-k+1];
return ans;
} int main()
{
char ch[2];
int i, m, k;
scanf("%d", &N);
for(i = 0; i < N; ++i)
{
scanf("%s", ch);
if(ch[0] == 'B') cow[i] = 1;
else cow[i] = 0;
}
M = N;
K = 1;
for(k = 1; k <= N; ++k)
{
m = cal(k);
if(m >= 0 && m < M)
{
M = m;
K = k;
}
}
printf("%d %d\n", K, M);
return 0;
}
ACM题目————Face The Right Way的更多相关文章
- ACM题目————中缀表达式转后缀
题目描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符在两 ...
- HDU ACM 题目分类
模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...
- ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]
原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...
- 有一种acm题目叫做,奇葩!
本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pi ...
- ACM题目————STL练习之求次数
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个 ...
- ACM题目————zoj问题
题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20322 解决:3560 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. ...
- ACM题目————又见拦截导弹
描述 大家对拦截导弹那个题目应该比较熟悉了,我再叙述一下题意:某国为了防御敌国的导弹袭击,新研制出来一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:它的第一发炮弹能够到达任意的高度,但是以后每一发炮 ...
- ACM题目————还是畅通工程
Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路 ...
- ACM题目————小A的计算器
Description 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-25分别由a-z表示. 现在小A要在这个操作系统上实现一个计算器,这 ...
- ACM题目————二叉树的遍历
一.二叉树的后序遍历: 题目描述 给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列.本题假设二叉树的结点数不超过1000 输入 输 入数据分为多组,第一行是测试数据的组数n,下面的n行 ...
随机推荐
- JavaEE Servlet 学习笔记
一.Servlet概述 1.什么是Servlet Servlet是一个运行在服务器端的Java小程序,通过HTTP协议用于接收来自客户端请求,并发出响应. 2.Servlet中的方法 public v ...
- Tarjan-LCA算法小记
Tarjan-LCA算法是一种离线算法. 算法描述: DFS遍历每个节点,对于遍历到的当前节点u: ①建立以u为代表元素的集合. ②遍历与u相连的节点v,如果没有被访问过,对于v使用Tarjan-LC ...
- WebWorker的浏览器"异步线程"
worker新线程: 1.通过postMessage( data ) 方法来向主线程发送数据; 2.绑定onmessage方法来接收主线程发送过来的数据: 3.worker外部文件不允许使用win ...
- Making the Grade---poj3666(dp)
题目链接:http://poj.org/problem?id=3666 题意:有一个n个数的序列a,现在要把这些序列变成单调增的或者单调减的序列 b , 其价值为|A1 - B1| + |A2 - B ...
- Jungle Roads---poj1251 hdu1301
Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...
- 自定义vueawesomeswiper分页器样式
swiperOption: {//swiper的配置项 notNextTick: true,//想获得swiper实例对象,这个必须为true direction: 'vertical', // gr ...
- AT2043 AND Grid 构造
正解:构造 解题报告: 传送门传送门! 这题psj讲了俩做法,一个是最常见的解法,还一种还不知道484对的QAQ 然后先把psj讲的不知正确性的做法港下QwQ 大概就是说,第一个图,先把底给染完 然后 ...
- PS快速制作下雪效果
PS快速制作下雪效果 具体的制作步骤如下: 1.打开PS,打开素材,打开窗口-动作 2.创建新动作,参数如下图 3.回到图层,建立一个图层,填充黑色,如下图 4.滤镜-像素化-点状化,参数如下图 5. ...
- 修改dedecms面包屑导航的首页链接关键字
dedecms面包屑导航默认是"主页>分类>二级分类>",我们知道链接的锚文字对排名有一定影响,这时可以考虑将“主页”改成具体的关键字,那么如何修改dedecms ...
- [py][mx]operation模型设计
用户表 UserMesasage 用户消息 UserFavorite 用户收藏 UserAsk 用户咨询 UserCourse 用户学习的课程 CourseComments 用户评论 收藏有3种类型 ...