• Codeforces Round #276 (Div. 1)D.Kindergarten DP贪心


    D. Kindergarten
     
     

    In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero).

    The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.

    Input

    The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106).

    The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).

    Output

    Print the maximum possible total sociability of all groups.

    Sample test(s)
    input
    5
    1 2 3 1 2
    output
    3
     
    Note

    In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1.

    In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group.

    题意:给你n个连续的数,让你划分成连续的区间,每个区间的价值为此区间内最大最小值之差,问你这n个数形成的最大价值是多少

    题解:贪心的一个思想就是单调必须在同一区间,知道这个就好做了

            考虑v形倒v形就好了

    ///1085422276
    
    #include<bits/stdc++.h>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std ;
    typedef long long ll;
    typedef unsigned long long ull;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){
            if(ch=='-')f=-1;ch=getchar();
        }
        while(ch>='0'&&ch<='9'){
            x=x*10+ch-'0';ch=getchar();
        }return x*f;
    }
    //****************************************
    const int  N=1000000+50;
    #define mod 10000007
    #define inf 1000000001
    #define maxn 10000
    
    int a[N];
    ll dp[N];
    
    int main() {
        int n=read();a[1]=0;
        for(int i=1;i<=n;i++) {
            scanf("%d",&a[i]);
        }dp[1]=0;
        for(int i=2;i<=n;i++) {
           if(a[i]>=a[i-1]&&a[i-1]>=a[i-2]) dp[i]=dp[i-1]+a[i]-a[i-1];
           else if(a[i]<=a[i-1]&&a[i-1]<=a[i-2]) dp[i]=dp[i-1]+a[i-1]-a[i];
           else if(a[i]>=a[i-1]&&a[i-1]<=a[i-2]) dp[i]=max(dp[i-2]+a[i]-a[i-1],dp[i-1]);
           else if(a[i]<=a[i-1]&&a[i-1]>=a[i-2]) dp[i]=max(dp[i-2]+a[i-1]-a[i],dp[i-1]);
        }
        cout<<dp[n]<<endl;
    
        return 0;
    }
    代码
  • 相关阅读:
    linux下编译GDAL3.x(集成Proj和Geos等)
    CSS后代选择器、子元素选择器、相邻兄弟选择器区别与详解
    CSS hover伪类改变其他元素的样式
    PHP 删除数组指定元素
    CakePHP 调试方法 汇总
    Linux/Mac OS 在终端使用 code 命令打开项目 VSCode
    苹果 MacOS 安装 Source Code Pro
    MacOS 安装 Homebrew 错误处理 Connection refused
    修改 VSCode 终端 Terminal 的字体
    spring cloud gateway自定义过滤器
  • 原文地址:https://www.cnblogs.com/zxhl/p/4965120.html
Copyright © 2020-2023  润新知