zoukankan      html  css  js  c++  java
  • [思维]Tree Burning

    题目描述

    Takahashi Lake has a perimeter of L. On the circumference of the lake, there is a residence of the lake's owner, Takahashi. Each point on the circumference of the lake has a coordinate between 0 and L (including 0 but not L), which is the distance from the Takahashi's residence, measured counter-clockwise.

    There are N trees around the lake; the coordinate of the i-th tree is Xi. There is no tree at coordinate 0, the location of Takahashi's residence.

    Starting at his residence, Takahashi will repeat the following action:

    If all trees are burnt, terminate the process.
    Specify a direction: clockwise or counter-clockwise.
    Walk around the lake in the specified direction, until the coordinate of a tree that is not yet burnt is reached for the first time.
    When the coordinate with the tree is reached, burn that tree, stay at the position and go back to the first step.
    Find the longest possible total distance Takahashi walks during the process.

    Partial Score
    A partial score can be obtained in this problem:
    300 points will be awarded for passing the input satisfying N≤2000.
    Constraints
    2≤L≤109
    1≤N≤2×105
    1≤X1<...<XN≤L−1
    All values in input are integers.

    输入

    Input is given from Standard Input in the following format:

    L N
    X1
    :
    XN
     

    输出

    Print the longest possible total distance Takahashi walks during the process.

    样例输入

    10 3
    2
    7
    9
    

    样例输出

    15
    

    提示

    Takahashi walks the distance of 15 if the process goes as follows:

    Walk a distance of  2 counter-clockwise, burn the tree at the coordinate 2 and stay there.
    Walk a distance of 5 counter-clockwise, burn the tree at the coordinate 7 and stay there.
    Walk a distance of 8 clockwise, burn the tree at the coordinate 9 and stay there.

     最优方案形如LLLL RLRL 或RRRR LRLR

    枚举起始方向和转折点

    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    typedef long long ll;
    using namespace std;
    
    int l,n,a[200005],d[200005];
    ll ans=0;
    
    void solve(){
      ll tot=0;
      for(int i=n;i>=1;i--){//枚举转折点i
        d[i]=a[i],tot+=d[i];//d[i]为相对原点(0)顺/逆时针走走到i点的距离,其初值设为从原点顺时针走到i点的距离
        int len=n-i+1,p;//p为转折点为i时的终点
        if(len&1) p=n-len/2;
        else{
            p=n-len/2+1;
            tot-=d[p];
            d[p]=l-d[p];//表明此时从原点逆时针走到p点
            tot+=d[p];
        }
        ans=max(ans,tot*2-d[p]);//除了终点p的d[p]外,每个d[n~i]都走了2遍
      }
    }
    
    int main()
    {
        scanf("%d%d",&l,&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        solve();//起始顺时针
        reverse(a+1,a+1+n);
        for(int i=1;i<=n;i++) a[i]=l-a[i];
        solve();//起始逆时针
        printf("%lld
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    ASP.NET Core 添加统一模型验证处理机制
    【Spark】开发Spark选择Java还是Scala?
    【设计模式】单例模式-为什么是静态变量
    【Spark】SparkStreaming-如何使用checkpoint
    【Java】Java-ShutDownHook-优雅关闭系统资源
    【Scala】Scala-None-null引发的血案
    【Spark】SparkStreaming-输出到Kafka
    【Spark】Spark-Redis连接池
    【Spark】提交Spark任务-ClassNotFoundException-错误处理
    【大数据】王加林-大数据学习资料
  • 原文地址:https://www.cnblogs.com/lllxq/p/10509842.html
Copyright © 2011-2022 走看看