D. Omkar and Circle
Posted vagrantac
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D. Omkar and Circle相关的知识,希望对你有一定的参考价值。
Danny, the local Math Maniac, is fascinated by circles, Omkar‘s most recent creation. Help him solve this circle problem!
You are given (n) nonnegative integers (a1,a2,…,an) arranged in a circle, where nn must be odd (ie. (n?1) is divisible by (2)). Formally, for all (i) such that (2≤i≤n), the elements (ai?1) and (ai) are considered to be adjacent, and (an) and (a1) are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.
Help Danny find the maximum possible circular value after some sequences of operations.
Input
The first line contains one odd integer (n) ((1≤n<2?10^5), (n) is odd) — the initial size of the circle.
The second line contains nn integers (a1,a2,…,an) ((0≤ai≤10^9)) — the initial numbers in the circle.
Output
Output the maximum possible circular value after applying some sequence of operations to the given circle.
Examples
input
3
7 10 2
output
17
input
1
4
output
4
Note
For the first test case, here‘s how a circular value of 1717 is obtained:
Pick the number at index 33. The sum of adjacent elements equals 1717. Delete 77 and 1010 from the circle and replace 22 with 1717.
Note that the answer may not fit in a 3232-bit integer.
题解
第一感觉,这个题和合并果子的那道题很像,可以尝试使用优先队列来写,合并的时候,优先合并当前位置元素小的,结果 (wa) 了。
优化一下需要求解的东西,
(n) 个物品,需要合并 (n/2) 次
每次选择合并的位置不能是相邻的,
(namo) 就可以选择以求前缀和的方式来求解不相邻的 (n/2) 个的最小值,拿总之减一下,或者是求一下减之后最大值。
代码
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 4e5+55;
ll a[maxn];
ll c[maxn];
ll d[maxn];
int main(){
int n;
ll sum=0;
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%lld",&a[i]);
sum+=a[i];
}
for(int i=0;i<n;++i){
if(i>=2)c[i]=c[i-2]+a[i];
else c[i]=a[i];
}
for(int i=n-1;i>=0;--i){
if(i<=n-3)d[i]=d[i+2]+a[i];
else d[i]=a[i];
}
ll maxx=c[0];
for(int i=0;i<n;++i){
maxx=max(maxx,c[i]+d[i+1]);
}
printf("%lld
",maxx);
return 0;
}
以上是关于D. Omkar and Circle的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 1372D Omkar and Circle
D. Omkar and Medians——Codeforces Round #724 (Div. 2)
D. Omkar and Medians——Codeforces Round #724 (Div. 2)
CodeforcesRound#749(Div.1+Div.2,basedonTechnocup2022EliminationRound1)-C. Omkar and Determination-题解