zoukankan      html  css  js  c++  java
  • 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;
    }
    代码
  • 相关阅读:
    反射+javacsv+scv文件构建资源获取
    数据库连接池学习
    标准web架构分层
    Luogu P1850换教室【期望dp】By cellur925
    bzoj1179: [Apio2009]Atm 【缩点+spfa最长路】
    Luogu P3946 ことりのおやつ(小鸟的点心) 【最短路】By cellur925
    Luogu P3393 逃离僵尸岛【最短路】By cellur925
    Luogu P4933 大师【dp】By cellur925
    莫队初探(不带修/例题极少)By cellur925
    分层图初探 By cellur925
  • 原文地址:https://www.cnblogs.com/zxhl/p/4965120.html
Copyright © 2011-2022 走看看