Codeforces 484D. Kindergarten DP贪心


贪心的选择单调的序列进行划分

dp处理拐点(gd)应该分到左边的单调序列还是右边的单调序列

大量读入加输入挂

D. Kindergarten
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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
input
3
3 3 3
output
0
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.




/* ***********************************************
Author        :CKboss
Created Time  :2015年03月10日 星期二 21时54分06秒
File Name     :CF484D_2.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

const int maxn=1009000;
int n;
LL a[maxn],dp[maxn];

LL getLL()
{
	char ch; bool flag=true;
	LL ret=0; LL fg=1;
	while(ch=getchar())
	{
		if((ch>='0'&&ch<='9')||ch=='-') 
		{
			if(flag) flag=false;
			if(ch=='-') fg=-1;
			else ret=ret*10LL+ch-'0';
		}
		else if(flag==false) break;
	}
	return ret*fg;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	scanf("%d",&n);
	getchar();
	for(int i=1;i<=n;i++) a[i]=getLL();

	int gd=1;
	bool up;
	if(a[2]>a[1]) up=true;
	else up=false;

	for(int i=2;i<=n;i++)
	{
		if(a[i-1]<a[i]) /// up
		{
			if(up==true)
			{
				dp[i]=max(a[i]-a[gd]+dp[gd-1],a[i]-a[gd+1]+dp[gd]);
			}
			else if(up==false) /// change gd
			{
				up=true; gd=i-1;
				dp[i]=max(dp[gd],dp[gd-1]+a[i]-a[gd]);
			}
		}
		else if(a[i-1]>a[i]) /// down
		{
			if(up==true) /// change gd
			{
				up=false; gd=i-1;
				dp[i]=max(dp[gd],dp[gd-1]+a[gd]-a[i]);
			}
			else if(up==false)
			{
				dp[i]=max(a[gd]-a[i]+dp[gd-1],a[gd+1]-a[i]+dp[gd]);
			}
		}
		else if(a[i-1]==a[i])
		{
			/// new gd
			gd=i; up=true;
			dp[i]=dp[i-1];
		}
	}
	cout<<dp[n]<<endl;
    return 0;
}




你可能感兴趣的:(Codeforces 484D. Kindergarten DP贪心)