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
  • 相关阅读:
    Ubuntu根底入门教程
    Debian发行版总结
    ARCH中KDEMOD下如何管理挂在NTFS分区乱码的标题问题
    让YUM窗口化
    操持SUSE Linux 10下无法显示阻碍一例
    让debian支撑鼠标中键
    电视片头后期合成软件、制作流程与技巧简介
    关于动态数组、静态数组转换为字符串的一些问题 给 "厨师" 的回复
    论证 Assigned(btn) 与 Assigned(@btn) 的区别 答复 "sunnet"
    WinAPI: GetRegionData
  • 原文地址:https://www.cnblogs.com/lllxq/p/10509842.html
Copyright © 2011-2022 走看看