• codeforces 545C Woodcutters


    题目:Woodcutters

    传送门:

    http://codeforces.com/contest/545/problem/C

    题目简介:给n棵树的在一维数轴上的坐标,以及它们的高度。现在要你砍倒这些树,树可以向左倒也可以向右倒,砍倒的树不能重合、当然也不能覆盖其他的树原来的位置,现在求最大可以砍倒的树的数目。

    分析:

    (1)对于第i棵树,可以不砍,可以砍了向左倒,可以砍了向右倒。我们得到三种状态,用f[i][0]表示不砍,f[i][1]表示砍了向左倒,f[i][2]表示砍了向右倒;

      (1.1)对于第i棵不砍的情况:显然f[i][0]可以由第(i-1)棵不砍、第(i-1)棵砍了向左倒这两种情况转移而来;如果第(i-1)棵砍了向右倒不会压到第i棵,则f[i][0]可以由第(i-1)棵砍了向右倒转移而来。

      (1.2)对于第i棵砍了向左倒的情况:如果第i棵砍了不会压到第(i-1)棵,则f[i][1]可以由第(i-1)棵不砍、第(i-1)棵砍了向左倒则两种情况转移而来;如果第(i-1)棵砍了向右倒且第i棵砍了向左倒不会互相压到,则f[i][1]可以由第(i-1)棵砍了向右倒转移而来。

      (1.3)对于第i棵砍了向右倒的情况:显然f[i][2]可以由第(i-1)棵不砍、第(i-1)棵砍了向左倒这两种情况转移而来;如果第(i-1)棵砍了向右倒不会压到第i棵,则f[i][2]可以由第(i-1)棵砍了向右倒转移而来。

    (2)在原题中有一句话“The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.”,可以在线做这道题;f[i]只与f[i-1]有关,可以滚动数组。

    (3)事实上,这道题有贪心的解法。第一棵树向左边倒;最后一棵树向右边倒;对于中间的树来说,优先向左边倒,如果左边距离不够的话才向右边倒,并更新一下距离。

    注意:

    (1)本题数据范围很大,设置-INF时不要设得太大。

    (2)本题数据范围很大,a+b+c<d会算数上溢,改为a+b<d-c就好了。

    代码:

    1)

     1 #include <cstdio>
     2 int n,x[100005],h[100005],f[100005][3];
     3 int max(int x,int y){x-=y;return y+(x&(~(x>>31)));}
     4 int main(){
     5     //freopen("in.txt","r",stdin);
     6     //freopen("out.txt","w",stdout);
     7     scanf("%d",&n);x[0]=-2e9;
     8     for(int i=1;i<=n;++i){
     9         scanf("%d%d",&x[i],&h[i]);
    10         f[i][0]=max(f[i-1][0],f[i-1][1]);
    11         if(x[i-1]+h[i-1]<x[i])f[i][0]=max(f[i][0],f[i-1][2]);
    12         if(x[i-1]<x[i]-h[i])f[i][1]=max(f[i-1][0],f[i-1][1])+1;
    13         if(x[i-1]+h[i-1]<x[i]-h[i])f[i][1]=max(f[i][1],f[i-1][2]+1);
    14         f[i][2]=max(f[i-1][0],f[i-1][1])+1;
    15         if(x[i-1]+h[i-1]<x[i])f[i][2]=max(f[i][2],f[i-1][2]+1);
    16     }
    17     printf("%d",max(max(f[n][0],f[n][1]),f[n][2]));
    18     //fclose(stdin);fclose(stdout);
    19     return 0;
    20 }

     2)

    #include <cstdio>
    int max(int x,int y){x-=y;return y+(x&(~(x>>31)));}
    int main(){
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int n;scanf("%d",&n);
        int x0=-2e9,x1,h0=0,h1;
        int f00=0,f01=0,f02=0,f10,f11,f12;
        for(int i=1;i<=n;++i){
            scanf("%d%d",&x1,&h1);
            f10=max(f00,f01);if(x0+h0<x1)f10=max(f10,f02);
            f11=0;if(x0<x1-h1)f11=max(f00,f01)+1;if(x0+h0<x1-h1)f11=max(f11,f02+1);
            f12=max(f00,f01);if(x0+h0<x1)f12=max(f12,f02);++f12;
            x0=x1;h0=h1;f00=f10;f01=f11;f02=f12;
        }
        printf("%d",max(max(f00,f01),f02));
        //fclose(stdin);fclose(stdout);
        return 0;
    }

     3)

    #include <cstdio>
    int main(){
        int ans=0;
        scanf("%*d");
        for(int x1=-(1<<30),h1=0,x,h;~scanf("%d %d",&x,&h);++ans,x1=x,h1=h){
            if(x1+h1>=x){--ans;h1=0;}
            if(x-h>x1+h1)h=0;
        }
        printf("%d",ans);
    }
  • 相关阅读:
    hadoop hdfs总结 NameNode部分 概述
    最近近况
    hadoop hdfs总结 NameNode部分 1
    rsync 使用
    SmartHost
    hadoop unit test 问题
    git 使用记录
    java 类内存分配计算
    hadoop hdfs总结 NameNode部分 2
    0417 430调试技巧
  • 原文地址:https://www.cnblogs.com/hjj1871984569/p/5621984.html
Copyright © 2020-2023  润新知