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;
    }
  • 相关阅读:
    蓝桥杯真题(1)
    蓝桥杯------历届真题
    第三届蓝桥杯 c/c++真题
    数据库三大范式个人理解,书上讲的太抽象
    c#线程池ThreadPool实例详解
    c#多线程thread实例详解
    C#中异步使用及回调
    c# 多线程的几种方式
    C#中委托的同步和异步有什么区别
    C#设计模式——单例模式的实现
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4607294.html
Copyright © 2011-2022 走看看