码迷,mamicode.com
首页 > 其他好文 > 详细

[思维]Tree Burning

时间:2019-03-11 13:06:12      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:term   def   find   除了   star   src   values   oss   col   

题目描述

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\n",ans);
    return 0;
}
View Code

 

[思维]Tree Burning

标签:term   def   find   除了   star   src   values   oss   col   

原文地址:https://www.cnblogs.com/lllxq/p/10509842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!