AGC 030 B - Tree Burning

时间:2022-09-11 12:40:39

B - Tree Burning

链接

题意:

  一个长度为L的环,有n个位置上有树,从0出发,每次选择一个方向(顺时针或者逆时针),一直走,直到走到一棵树的位置,烧掉这棵树,重复这个过程,直到没有树。求最多走多少距离。

分析:

  最优一定是LLLRLRLRL……类似这样的,于是枚举每个点,计算答案。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; LL s1[N], s2[N], a[N], L, Ans;
int n; LL f(int l,int r) {
return s1[r] - a[l] * (r - l) - s1[l];
} void Calc() {
for (int i = ; i <= n; ++i)
s1[i] = s1[i - ] + a[i], s2[i] = s2[i - ] + (L - a[n - i + ]);
for (int i = ; i < n; ++i) {
LL now = a[i] * (n - i) + a[i];
int cnt = (n - i) / ;
if ((n - i) % == )
now += s2[cnt] * + f(i, i + cnt) * - (a[i + cnt] - a[i]);
else
now += s2[cnt + ] * + f(i, i + cnt) * - (L - a[n - cnt]);
Ans = max(Ans, now);
}
}
int main() {
L = read(); n = read();
for (int i = ; i <= n; ++i) a[i] = read();
Calc();
for (int i = ; i <= n; ++i) a[i] = L - a[i];
reverse(a + , a + n + );
Calc();
cout << Ans;
return ;
}