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行 ...
随机推荐
- background-size:100% 100% 时 background-position: % 失效
背景知识: background-size background-position 难题: background-size 为 100% 100% 时,background-position 部分失效 ...
- Python重要基础点
这里记录一些实际编写代码时遇到的常用/重要的内容,有前辈的,也有自己整理加进来的. 以点的形式记录,之后遇到的内容会慢慢补充进来 1. 斜杠 / :斜字第一笔, 转义用反斜杠 \ : 转的最后一笔 2 ...
- bootstrapValidator remote 的接受 验证 值
本来之前也做过一次这样的验,但可能是这两天太热脑袋不够用了,于是就只有看看源码咯 that.updateStatus(updateAll ? $f.attr('data-bv-field') : $f ...
- Freemarker 基本数据类型
一 数据类型简介 freemarker 模板中的数据类型由如下几种: 1. 布尔型:等价于java中的boolean类型, 不同的是不能直接输出,可以转换成字符串再输出 2. 日期型:等价于java中 ...
- UOJ 145 - 神奇的幻方 - [简单数学题]
题目链接:http://uoj.ac/problem/145 题目描述 幻方是一种很神奇的 N∗N 矩阵:它由数字 1,2,3,⋯⋯,N×N 构成,且每行.每列及两条对角线上的数字之和都相同. 当 N ...
- 43(function pointer 1)
#include<iostream> using namespace std; typedef int A; typedef void (*PF)(); typedef int (*P_A ...
- 币安Binance API Websocket
本文介绍币安Binance API Websocket General WSS information The base endpoint is: wss://stream.binance.com:9 ...
- Number Sequence--POJ1019
Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35251 Accepted: 10151 ...
- Eclipse+Pydev 搭建开发环境(转)
add by zhj: 对原文略有修改 原文:http://www.cnblogs.com/TankXiao/archive/2013/05/29/3033640.html C#之所以容易让人感兴趣, ...
- VUX
1.mapState 2.mapGetters 3. 4. 当你的操作行为中含有异步操作,比如向后台发送请求获取数据,就需要使用action的dispatch去完成.其他使用commit即可. 举个例 ...