• 原题如下:

    Face The Right Way
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6708   Accepted: 3124

    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

    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

    7
    B
    B
    F
    B
    F
    B
    B

    Sample Output

    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)
  • 题解:对于一个特定的K如何求出让所有的牛面朝前方的最小操作次数?首先,交换区间反转的顺序对结果是没有影响的,另外,可以知道对同一个区间进行两次以上的反转是多余的,由此,问题就转化成了求需要反转的区间的集合。先考虑一下最左端的牛,因为包含这头牛的区间只有一个,所以如果这头牛面朝前方,我们就能知道这个区间不需要反转,反之如果这头牛面朝后方,对应的区间就必须进行反转了,而且在此之后的最左区间就再也不需要考虑了,这样一来通过首先考虑最左端的牛,问题的规模就缩小了1,不断地重复下去,就可以无需搜索求出最少所需反转次数了。通过前面的分析可以知道,忽略掉对同一个区间重复反转这类多余操作之后,只要存在让所有的牛都朝前的方法,那么操作就和顺序无关,可以唯一确定了。对于整个算法的话,我们需要对所有的K都求解一次,对于每个K最坏情况下需要进行N-K+1次的反转操作,每次操作又要反转K头牛,于是总的复杂度是O(N3)。区间反转的部分还可以进行优化,令f[i]:=区间[i,i+K-1]进行了反转的话为1,否则为0,这样在考虑第i头牛时,如果∑(j=i-K+1,i-1)f[j]为奇数的话,则这头牛的方向与起始方向是相反的,否则方向不变,由于∑(j=(i+1)-K+1,i)f[j] = ∑(j=i-K+1,i-1)f[j]+f[i]-f[i-K+1],所以这个和每一次都可以用常数时间计算出来,复杂度就降为了O(N2),就可以在时限内解决问题了。
  • 代码:
     #include <cstdio>
    #include <cctype>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #define num s-'0' using namespace std; const int MAX_N=;
    const int INF=0x3f3f3f3f;
    int N;
    int dir[MAX_N], f[MAX_N]; void read(int &x){
    char s;
    x=;
    bool flag=;
    while(!isdigit(s=getchar()))
    (s=='-')&&(flag=true);
    for(x=num;isdigit(s=getchar());x=x*+num);
    (flag)&&(x=-x);
    } void write(int x)
    {
    if(x<)
    {
    putchar('-');
    x=-x;
    }
    if(x>)
    write(x/);
    putchar(x%+'');
    } int calc(int); int main()
    {
    read(N);
    for (int i=; i<N; i++)
    {
    char d;
    scanf("%c", &d);
    d=='B'? dir[i]=: dir[i]=;
    scanf("%c", &d);
    }
    int K=,M=N;
    for (int k=N; k>=; k--)
    {
    int m=calc(k);
    if (m>= && m<M)
    {
    M=m;
    K=k;
    }
    }
    write(K);putchar(' ');write(M);putchar('\n');
    } int calc(int K)
    {
    memset(f, , sizeof(f));
    int res=;
    int sum=;
    for (int i=; i<=N-K; 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;
    }

Face The Right Way(POJ 3276)的更多相关文章

  1. 反转(开关问题) POJ 3276

    POJ 3276 题意:n头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全部面朝前方.问:求最小操作m,再此基础上求k. 题解:1.5000头 ...

  2. POJ 3276 (开关问题)

    题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...

  3. poj 3276(反转)

    传送门:Problem 3276 参考资料: [1]:挑战程序设计竞赛 先献上AC代码,题解晚上再补 题意: John有N头牛,这些牛有的头朝前("F"),有的朝后("B ...

  4. POJ 3276 Face The Right Way 反转

    大致题意:有n头牛,有些牛朝正面,有些牛朝背面.现在你能一次性反转k头牛(区间[i,i+k-1]),求使所有的牛都朝前的最小的反转次数,以及此时最小的k值. 首先,区间反转的顺序对结果没有影响,并且, ...

  5. Enum:Face The Right Way(POJ 3276)

    面朝大海,春暖花开 题目大意:农夫有一群牛,牛排成了一排,现在需要把这些牛都面向正确的方向,农夫买了一个机器,一次可以处理k只牛,现在问你怎么处理这些牛才可以使操作数最小? 这道题很有意思,其实这道题 ...

  6. POJ 3276

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2193   Accepted: 103 ...

  7. Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276

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

  8. POJ 3276 Face The Right Way 翻转(开关问题)

    题目:Click here 题意:n头牛排成一列,F表示牛面朝前方,B表示面朝后方,每次转向K头连续的牛的朝向,求让所有的牛都能面向前方需要的最少的操作次数M和对应的最小的K. 分析:一个区间反转偶数 ...

  9. POJ 3276 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 ...

  10. POJ 3276 Face The Right Way(前缀和优化)

    题意:有长度为N的01串,有一个操作可以选择连续K个数字取反,求最小的操作数和最小的K使得最后变成全1串.(N<=5000) 由于K是不定的,无法高斯消元. 考虑枚举K,求出最小的操作数. 显然 ...

随机推荐

  1. C#LeetCode刷题之#69-x 的平方根(Sqrt(x))

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3848 访问. 实现 int sqrt(int x) 函数. 计算 ...

  2. iOS Abort问题系统性解决方案

    一.背景 崩溃(Crash),即闪退,多指移动设备(如iOS.Android设备)在打开/使用应用程序的过程中,突然出现意外退出/中断的情况.如果App线上版本频繁发生崩溃,会极大地影响用户体验,甚至 ...

  3. SpringBoot项目 使用Jenkins进行自动化部署 gitlab打tag 生产测试环境使用 含配置中心

    脚本 node('master') { def mvnHome = tool 'maven11-free' def gitUrl = "http://gitlab.wdcloud.cc:10 ...

  4. 【转】camera tuning

    chromatix项目必须包含有效的 ADC image.png filcker: 交流电照明灯发出的光会一定频率的抖动导致sensor图像出现行方向的水波纹,称之为filcker.目前主要的交流电频 ...

  5. 6. 二十不惑,ObjectMapper使用也不再迷惑

    一滴水,用显微镜看,也是一个大世界.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习.关注公众 ...

  6. 牛客网数据库SQL实战解析(21-30题)

    牛客网SQL刷题地址: https://www.nowcoder.com/ta/sql?page=0 牛客网数据库SQL实战解析(01-10题): https://blog.csdn.net/u010 ...

  7. C# 根据出生年月 计算天数/计算X岁X月X天字符串

    public class TimeTool { //根据出生年月计算 整数天 private static int GetAgeByBirthdate(DateTime birthdate) { Da ...

  8. Linux操作命令和工具使用

    lrzsz是一个unix通信套件提供的X,Y,和ZModem文件传输协议. windows 需要向centos服务器上传文件,可直接在centos上执行命令yum -y install lrzsz 程 ...

  9. 从String类型发散想到的一些东西

    值类型 引用类型 值类型表示存储在栈上的类型,包括简单类型(int.long.double.short).枚举.struct定义: 引用类型表示存在堆上的类型,包括数组.接口.委托.class定义: ...

  10. SpringBoot2搭建基础架构——开源软件诞生4

    技术框架篇--第4篇 用日志记录“开源软件”的诞生 赤龙ERP开源地址: 点亮星标,感谢支持,加微信与开发者交流 kzca2000 码云:https://gitee.com/redragon/redr ...