zoukankan      html  css  js  c++  java
  • 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;
    }
    
    新赛季的开始
  • 相关阅读:
    每日记载内容总结33
    华为机试-尼科彻斯定理
    华为机试-求最大连续bit数
    华为机试-合法IP
    华为机试-票数统计
    华为机试-等差数列
    华为机试-自守数
    中序表达式转后序表式式
    华为机考 给你一个N*M的矩阵,每个位置的值是0或1,求一个面积最大的子矩阵,这个矩阵必须是一个正方形,且里面只能由1构成,输出最大的正方形边长。其中n,m<=400;
    华为机试-求解立方根
  • 原文地址:https://www.cnblogs.com/VagrantAC/p/13287236.html
Copyright © 2011-2022 走看看