zoukankan      html  css  js  c++  java
  • Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/problemset/problem/484/D

    Description

    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 Input

    5
    1 2 3 1 2

    Sample Output

    3

    HINT

    题意

    给你n个数,然后让你分成若干组,只有连续的才能分成一组

    然后每组的分数是,这组的最大值减去这组的最小值

    问你最大能拿多少分

    题解:

    很显然单调的必然会分在一个组,这儿有一个问题就是拐点怎么办,分在左边还是右边?

    那就dp咯

    代码

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)
    #define maxn 1000005
    #define mod 10007
    #define eps 1e-5
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    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;
    }
    //**************************************************************************************
    
    ll dp1[maxn];
    ll dp2[maxn];
    ll dp3[maxn];
    int a[maxn];
    int main()
    {
        int n=read();
        for(int i=0;i<=n;i++)
            dp1[i]=dp2[i]=dp3[i]=-infll;
        for(int i=1;i<=n;i++)
            a[i]=read();
        dp1[1]=a[1];
        dp2[1]=-a[1];
        dp3[1]=0;
        for(int i=2;i<=n;i++)
        {
            dp3[i]=max(dp3[i-1],max(dp1[i-1]-a[i],dp2[i-1]+a[i]));
            dp1[i]=max(dp1[i-1],dp3[i-1]+a[i]);
            dp2[i]=max(dp2[i-1],dp3[i-1]-a[i]);
        }
        cout<<dp3[n]<<endl;
        return 0;
    }
  • 相关阅读:
    scala 获取当前时间的两种方式
    log4j配置输出日志文件
    scala读取jar包外配置文件的方式
    scala 异常处理机制
    IDEA 安装scala插件
    第2部分 Elasticsearch查询-请求体查询、排序
    第1部分 Elasticsearch基础
    2.css
    1.html
    Linux搭建Nexus仓库+高可用方案
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4607294.html
Copyright © 2011-2022 走看看