AGC 030 B - Tree Burning

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值