Codeforces Round #655 (Div. 2) D. Omkar and Circle ###K ###K //K
题目链接:https://codeforces.ml/contest/1372/problem/D
题意:给n个数 (第一个数和最后一个数算相邻 也就是构成环) 可以选择任意一个位置,使得这个位置左右两个数相加 代替原来这个位置上的数
重复操作使得只剩下一个数 问这个数最大为多少
思路:首先发现一个数 合并后 ,这个位置就不应该在用一次合并,否则肯定不是最大,
所以数要选不相邻的 选最小的n/2 个 ,发现这样很难实现, 破环成链后 写出写了所有的可能出来,发现答案一定是从相邻的数中取n/2+1个
维护前缀和即可o(n) 求得
如 1 2 3 4 5 成环后 1 2 3 4 5 1 2 3 4 5 从第6个数开始往前间隔取数 1+4+2 2+5+3 3+1+4 4+2+5 5+3+1 只有这5种可能
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define ull unsigned long long 5 #define pb push_back 6 const int maxn=2e5+10; 7 const int mod=998244353; 8 ll a[maxn*2]; 9 ll sum[maxn*2]; 10 11 int main() 12 { 13 ios::sync_with_stdio(false); 14 cin.tie(0); 15 int n; 16 cin>>n; 17 for(int i=1;i<=n;i++) 18 { 19 cin>>a[i]; 20 a[i+n]=a[i]; 21 } 22 ll ans=0; 23 sum[1]=a[1]; 24 for(int i=2;i<=2*n;i++) 25 { 26 sum[i]=sum[i-2]+a[i]; 27 } 28 for(int i=n+1;i<=n*2;i++) 29 { 30 ans=max(ans,sum[i]-sum[i-n-1]); 31 } 32 cout<<ans<<'\n'; 33 34 35 36 37 38 39 40 41 }

浙公网安备 33010602011771号