Face The Right Way
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2899   Accepted: 1338

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 ≤ KN)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

  1. 7
  2. B
  3. B
  4. F
  5. B
  6. F
  7. B
  8. B

Sample Output

  1. 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)
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <sstream>
  13. #include <iomanip>
  14. using namespace std;
  15. const int INF=0x4fffffff;
  16. const int EXP=1e-;
  17. const int MS=;
  18.  
  19. int dir[MS];
  20. int flag[MS]; // [i->i+k-1] 这个区间是否翻转。
  21. int N;
  22.  
  23. int calc(int k)
  24. {
  25. int sum=,res=;
  26. memset(flag,,sizeof(flag));
  27. for(int i=;i+k-<N;i++)
  28. {
  29. if((dir[i]+sum)&)
  30. {
  31. flag[i]=;
  32. res++;
  33. }
  34. sum+=flag[i];
  35. if(i-k+>=)
  36. sum-=flag[i-k+];
  37. }
  38. // N-k+1 --> N-1 这个区间的任意一个位置起无法翻转。
  39. // 需要检查这段区间是不是全部朝前。
  40. for(int i=N-k+;i<N;i++)
  41. {
  42. if((dir[i]+sum)&)
  43. return -;
  44. if(i-k+>=)
  45. sum-=flag[i-k+];
  46. }
  47. return res;
  48. }
  49.  
  50. void solve()
  51. {
  52. int K=,M=N;
  53. for(int k=;k<=N;k++)
  54. {
  55. int cnt=calc(k);
  56. if(cnt>=&&M>cnt)
  57. {
  58. M=cnt;
  59. K=k;
  60. }
  61. }
  62. printf("%d %d\n",K,M);
  63. }
  64.  
  65. int main()
  66. {
  67. char s[];
  68. scanf("%d",&N);
  69. for(int i=;i<N;i++)
  70. {
  71. scanf("%s",s);
  72. if(s[]=='F')
  73. dir[i]=;
  74. else
  75. dir[i]=;
  76. }
  77. solve();
  78. return ;
  79. }

Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276的更多相关文章

  1. HDU - 6103 :Kirinriki(不错的尺取法)

    We define the distance of two strings A and B with same length n is dis A,B =∑ i=0 n−1 |A i −B n−1−i ...

  2. poj 3320 技巧/尺取法 map标记

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  3. 刷题向》关于一道尺取法的神题目(BZOJ4653)(HARD-)(BZOJ 30题纪念)

    不得不说,这也许会是一道长期在我的博客里作为“HARD”难度存在的题 这道题能很好的考验选手的思考能力,但本蒟蒻最后还是听了省队爷讲了之后才会...(默默面壁) 题目里,说对于每一个点,是用当前选出的 ...

  4. HDU5806:NanoApe Loves Sequence Ⅱ(尺取法)

    题目链接:HDU5806 题意:找出有多少个区间中第k大数不小于m. 分析:用尺取法可以搞定,CF以前有一道类似的题目. #include<cstdio> using namespace ...

  5. POJ 3061 Subsequence【二分答案】||【尺取法】

    <题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...

  6. codeforces #364c They Are Everywhere 尺取法

    C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. POJ3320 尺取法的正确使用法

    一.前言及题意: 最近一直在找题训练,想要更加系统的补补思维,补补漏洞什么的,以避免被个类似于脑筋急转弯的题目干倒,于是在四处找书,找了红书.蓝书,似乎都有些不尽如人意.这两天看到了日本人的白书,重新 ...

  8. 【算法•日更•第二十三期】数据结构:two-pointer(尺取法)&莫队

    ▎引入 ☞『例题』 一道十分easy的题: 洛谷P1638 长度为n的序列,m种数 找一个最短区间,使得所有数出现一遍 n≤1e6 ,m≤2e3. ☞『分析』 这道题非常的简单,但是如果不会two-p ...

  9. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

随机推荐

  1. 三、 将DataTable 转换为List

    1. 方法public static IList<T> ConvertTo<T>(DataTable table) { if (table == null) { return ...

  2. jdk5下载链接

    查看jdk版本 java -version JDK下载 最新版本 http://www.oracle.com/technetwork/java/javase/downloads/index.html ...

  3. class-dump-z下载地址

    支持MAC.Linux.Win和iOS各个版本 http://download.csdn.net/detail/yukang1989/8414567

  4. win2008 64位 + oracle11G 64位 IIS7.5 配置WEBSERVICE

    第一个错误: 安装过程依旧是那样简单,但在配好IIS站点,准备连接数据库的时候出错了,以下是错误提示:System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更 ...

  5. AFNetWorking3.0使用 自签名证书的https请求

    前几日,项目组出于安全角度的考虑,要求项目中的请求使用https请求,因为是企业内部使用的app,因此使用了自签名的证书,而自签名的证书是不受信任的,所以我们就需要自己来做证书的验证,包括服务器验证客 ...

  6. GetSafeHwnd()函数解释[转]

    当我们想得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数,通过下面的例子来看其理由: CWnd *pwnd = FindWindow(“ ...

  7. spring 切面 前置后置通知 环绕通知demo

    环绕通知: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  8. C++经典面试题

    1.int a=5,则 ++(a++)的值是() A.5      B.   6          C.7       D.逻辑错误 a++返回的是一个暂时变量,这里是右值,不能再前面++了 2.以下 ...

  9. ASP.NET中上传并读取Excel文件数据

    在CSDN中,经常有人问如何打开Excel数据库文件.本文通过一个简单的例子,实现读取Excel数据文件. 首先,创建一个Web应用程序项目,在Web页中添加一个DataGrid控件.一个文件控件和一 ...

  10. Codeforces Round #340 (Div. 2) B. Chocolate 水题

    B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...