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;
    }
    代码
  • 相关阅读:
    CCF-CSP201512-3 画图
    CCF-CSP201512-2 消除类游戏
    CCF-CSP201606-4 游戏(BFS)
    CCF-CSP201604-2 俄罗斯方块
    HDU1035 Robot Motion(dfs)
    Java Srting之Calendar日历类(五)——Calendar中计算时间的方法add()
    java如何获取当前日期和时间
    double 类型怎样不用科学计数法表示并且使用Java正则表达式去掉Double类型的数据后面多余的0
    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})注解作用
    java.util.Date.toString()方法实例
  • 原文地址:https://www.cnblogs.com/zxhl/p/4965120.html
Copyright © 2011-2022 走看看