[思维]Tree Burning
Posted lllxq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[思维]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.
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
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; }
以上是关于[思维]Tree Burning的主要内容,如果未能解决你的问题,请参考以下文章
AGC 030 B - Tree Burning 结论+枚举
Codeforces 931D Peculiar apple-tree(dfs+思维).cpp