AGC 030 B

B - Tree Burning

链接

题意:

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

分析:

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

代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<cmath>
 6 #include<cctype>
 7 #include<set>
 8 #include<queue>
 9 #include<vector>
10 #include<map>
11 using namespace std;
12 typedef long long LL;
13 
14 inline int read() {
15     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
16     for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
17 }
18 
19 const int N = 200005;
20 
21 LL s1[N], s2[N], a[N], L, Ans;
22 int n;
23 
24 LL f(int l,int r) {
25     return s1[r] - a[l] * (r - l) - s1[l];
26 }
27 
28 void Calc() {
29     for (int i = 1; i <= n; ++i) 
30         s1[i] = s1[i - 1] + a[i], s2[i] = s2[i - 1] + (L - a[n - i + 1]);
31     for (int i = 0; i < n; ++i) {
32         LL now = a[i] * (n - i) + a[i];
33         int cnt = (n - i) / 2;
34         if ((n - i) % 2 == 0) 
35             now += s2[cnt] * 2 + f(i, i + cnt) * 2 - (a[i + cnt] - a[i]);
36         else 
37             now += s2[cnt + 1] * 2 + f(i, i + cnt) * 2 - (L - a[n - cnt]);
38         Ans = max(Ans, now);
39     }
40 }
41 int main() {
42     L = read(); n = read();
43     for (int i = 1; i <= n; ++i) a[i] = read();
44     Calc();
45     for (int i = 1; i <= n; ++i) a[i] = L - a[i];
46     reverse(a + 1, a + n + 1);
47     Calc();
48     cout << Ans;
49     return 0;
50 }
原文地址:https://www.cnblogs.com/mjtcn/p/10200198.html