Face The Right Way POJ - 3276 (开关问题)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6707 | Accepted: 3123 |
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
Sample Input
- 7
- B
- B
- F
- B
- F
- B
- B
Sample Output
- 3 3
Hint
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cmath>
- #include<cstdlib>
- #include<queue>
- #include<set>
- #include<map>
- #include<vector>
- using namespace std;
- #define INF 0x3f3f3f3f
- #define eps 1e-10
- typedef long long ll;
- const int maxn = 5e3+;
- const int mod = 1e9 + ;
- int gcd(int a, int b) {
- if (b == ) return a; return gcd(b, a % b);
- }
- int N,M;
- int dir[maxn],f[maxn]; //牛的方向 F:0 B:1
- int calc(int K)
- {
- memset(f,,sizeof(f));
- int res=,sum=;
- for(int i=;i+K<=N;i++)
- {
- if((dir[i]+sum)%!=)
- {
- res++;
- f[i]=;
- }
- sum+=f[i];
- if(i-K+>=)
- sum-=f[i-K+];
- }
- for(int i=N-K+;i<N;i++) //检查剩下的牛是否有面朝后方的情况
- {
- if((dir[i]+sum)%!=)
- return -;
- if(i-K+>=)
- sum-=f[i-K+];
- }
- return res;
- }
- void solve()
- {
- int K=;
- int M=N;
- for(int k=;k<=N;k++)
- {
- int m=calc(k);
- if(m>= && M>m)
- {
- M=m;
- K=k;
- }
- }
- cout<<K<<" "<<M<<endl;
- }
- int main()
- {
- scanf("%d",&N);
- int num=;
- for(int i=;i<N;i++){
- char ch;
- cin>>ch;
- if(ch=='B')
- dir[num]=;
- else
- dir[num]=;
- // cout<<dir[num]<<" ";
- num++;
- }
- solve();
- return ;
- }
Face The Right Way POJ - 3276 (开关问题)的更多相关文章
- POJ 3276 (开关问题)
题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...
- 反转(开关问题) POJ 3276
POJ 3276 题意:n头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全部面朝前方.问:求最小操作m,再此基础上求k. 题解:1.5000头 ...
- POJ 3276 Face The Right Way 翻转(开关问题)
题目:Click here 题意:n头牛排成一列,F表示牛面朝前方,B表示面朝后方,每次转向K头连续的牛的朝向,求让所有的牛都能面向前方需要的最少的操作次数M和对应的最小的K. 分析:一个区间反转偶数 ...
- POJ 1681 (开关问题+高斯消元法)
题目链接: http://poj.org/problem?id=1681 题目大意:一堆格子,或白或黄.每次可以把一个改变一个格子颜色,其上下左右四个格子颜色也改变.问最后使格子全部变黄,最少需要改变 ...
- POJ 1222 (开关问题+高斯消元法)
题目链接: http://poj.org/problem?id=1222 题目大意:一堆开关,或开或关.每个开关按下后,周围4个方向开关反转.问使最后所有开关都关闭的,开关按法.0表示不按,1表示按. ...
- poj 1830 开关问题
开关问题 题意:给n(0 < n < 29)开关的初始和最终状态(01表示),以及开关之间的关联关系(关联关系是单向的输入a b表示a->b),问有几种方式得到最终的状态.否则输出字 ...
- POJ 1830 开关问题(高斯消元)题解
思路:乍一看好像和线性代数没什么关系.我们用一个数组B表示第i个位置的灯变了没有,然后假设我用u[i] = 1表示动开关i,mp[i][j] = 1表示动了i之后j也会跟着动,那么第i个开关的最终状态 ...
- POJ 1830 开关问题(Gauss 消元)
开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7726 Accepted: 3032 Description ...
- POJ 1830 开关问题 【01矩阵 高斯消元】
任意门:http://poj.org/problem?id=1830 开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...
随机推荐
- android 开发AlertDialog.builder对话框的实现
AndroidAPI提供了Dialog对话框控件,但google明确指出不建议开发者只是使用Dialog来创建对话框,而应该自定义对话框或者使用API中提供的Dialog的子类,如AlertDialo ...
- 自己动手实现STL 03:内存基本处理工具(stl_uninitialized.h)
一.前言 前面两篇已经编写了内存配置器和建构解构工具函数.这里,就准备编写并介绍下内存基本处理工具函数.比如uninitialized_copy().uninitialized_copy和 unini ...
- 数据库(JDBC、DBUtils)
JDBC(Java DataBase Connection) 今日内容介绍 u SQL语句查询 u JDBC 第1章 JDBC 1.1 JDBC概述 JDBC(Java Data Base Conn ...
- bootstrap Table的 一些小操作
function HQCreatTable(ob) { var option = { cache: false,//是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*) scroll ...
- Android rxjava2的disposable
rxjava+retrofit处理网络请求 在使用rxjava+retrofit处理网络请求的时候,一般会采用对观察者进行封装,实现代码复用和拓展.可以参考我的这篇文章:rxjava2+retrofi ...
- 青松云安全-WAF-1.0.655 (ubuntu 14.04 server)
平台: Ubuntu 类型: 虚拟机镜像 软件包: web application firewall basic software security ubuntu waf 服务优惠价: 按服务商许可协 ...
- 无法通过CTRL+空格及SHIFT+CTRL调出输入法的解决方案
打开任务管理器: 运行:CTFMON.EXE
- 两数相除赋值整数变量(T-SQL)
MSSQL: DECLARE @_pagecount INT; ; SELECT @_pagecount; 结果为1 Mysql: BEGIN DECLARE _pagecount INT; ; SE ...
- bzoj3312: [Usaco2013 Nov]No Change
题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱 ...
- Objective-C 引用计数原理
http://www.cocoachina.com/ios/20160112/14933.html 引用计数如何存储 有些对象如果支持使用 TaggedPointer,苹果会直接将其指针值作为引用计数 ...