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行 ...
随机推荐
- ElasticSearch 安装 elasticsearch-analysis-ik分词器
IK version ES version master 5.x -> master 5.6.1 5.6.1 5.5.3 5.5.3 5.4.3 5.4.3 5.3.3 5.3.3 5.2.2 ...
- POJ 3368 & UVA 11235 - Frequent values
题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...
- SQL Fundamentals || Single-Row Functions || 转换函数 Conversion function
SQL Fundamentals || Oracle SQL语言 SQL Fundamentals: Using Single-Row Functions to Customize Output使 ...
- Spring Boot中的AutoConfiguation核心注解
import org.springframework.boot.autoconfigure.condition.*; @ConditionalOnBean // 当容器中有指定bean的条件下 @Co ...
- rank() over,dense_rank(),row_number() 的区别
转自:https://jingyan.baidu.com/article/597035521ff2ec8fc107404b.html rank() over是的作用是查出指定条件后进行一个排名,但是有 ...
- OKEx货币对价格数量长度及精度
长度 precisions = [["bch_btc","0.001","0.00000001"], ["ltc_btc" ...
- matplotlib基本使用方法
[微语]人生有可为之事,也有不可为之事.可为之事,当尽力为之,此谓尽性,不可为之事,当尽心为之,此谓知命. 三人行必有我师 官方参考API:https://matplotlib.org/tutoria ...
- Linux和Windows下查看环境变量方法(转)
add by zhj: 本文中的Linux是指Ubuntu14.04 以前我对环境变量有误解,以为环境变量就是PATH这个变量.其实环境变量其实有很多,PATH仅仅是其中一个而已,比如在Windows ...
- 你可能用得到的9段CSS代码
一.opacity兼容 .transparent { filter: alpha(opacity=50);/* internet explorer */ -khtml-opacity: 0 ...
- 第一课 JDK环境变量配置
第一步:下载,并解压到D:/JDK 第二步:环境变量配置 右键我的电脑->属性->高级->环境变量->系统变量(注意:是下面的系统变量,不是上面的用户变量) 新建变量名 JAV ...