传送门
题意: 给一个长为nnn的数组(nnn为奇数),iii与i−1i-1i−1相邻,111与nnn相邻,每次选择一个位置,将这个位置的值变成与它相邻的两个位置的和,让后将相邻位置删掉。求最终剩下一个数的时候最大值是多少。
思路: 首先贪心是不行的,不能每次选最小的哪个把它替换成相邻的和。
由于nnn为奇数,所以要取最大值的话一定是选择了n+12\frac{n+1}{2}2n+1个数,在破环成链后,一定是n+12\frac{n+1}{2}2n+1个互不相邻的数,比如一下序列:
1 2 3 4 5 1 2 3 4 5
选n+12\frac{n+1}{2}2n+1个数的总情况为:
1 3 5
2 4 1
3 5 2.
4 1 3
5 2 4.
显然这个式子可以分奇偶来做,我们利用滑动窗口来滑就好啦。
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
int a[N];int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);LL sum=0,t=0;LL ans=0;scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i],t+=(i%2==1)*a[i];ans=max(ans,t);//首先是全奇数,即1与n相邻for(int i=2;i<=n;i++){t=sum-t+a[i-1];ans=max(ans,t);}printf("%lld\n",ans);return 0;
}
/**/