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

 

转载于:https://www.cnblogs.com/lllxq/p/10509842.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值