Maximum cost of 51nod 1270 arrays

Source: Internet
Author: User

1270 the maximum cost of an array
Title Source: Hackerrank
Base time limit: 1 seconds space limit: 131072 KB score: 20 Difficulty: 3-level algorithm problemarray A consists of n elements A1, A2 ... An. Array B consists of n elements B1, B2 ... BN. And each element of the array, AI, satisfies 1 <= ai <= Bi. The cost of array A is defined as follows:  (the formula represents the sum of the absolute values of the difference of all two adjacent elements)give the array b to calculate the possible maximum cost S. Input
Line 1th: 1 number N, indicating the length of the array (1 <= n <= 50000). Line 2-n+1:1 numbers per line, corresponding to array element bi (1 <= bi <= 10000).
Output
The maximum cost of output s.
Input Example
510110110
Output Example
36

/*51nod the maximum cost of an array of 1270 problem: array A contains n elements A1, A2 ... An. Array B consists of n elements B1, B2 ... BN. And each element of the array, AI, satisfies 1 <= ai <= Bi.  The cost of array A is defined as follows: The sum of the absolute values of the difference of two adjacent elements a solve: according to the greedy idea, the feeling must be take b[i] or 1. So the situation of step I should be pushed by i-1. But when writing, I think if the first bit if the maximum value, then i-1 take the minimum value mdzz, so there are a few cases. In fact, the maximum minimum value of i-1 should be considered. hhh-2016/09/03 13:32:28*/#pragma COMMENT (linker, "/stack:124000000,124000000") #include < algorithm> #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring># Include <vector> #include <math.h> #include <queue> #include <set> #include <map> #define Lson i<<1#define Rson i<<1|1#define ll long Long#define CLR (A, B) memset (A,b,sizeof (a)) #define SCANFI (a) scan F ("%d", &a) #define SCANFS (a) scanf ("%s", a) #define SCANFL (a) scanf ("%i64d", &a) #define SCANFD (a) scanf ("%lf",  &a) #define KEY_VAL ch[ch[root][1]][0] #define EPS 1e-7#define inf 0x3f3f3f3f3f3f3f3fusing namespace std;const ll mod = 1e9+7;const int maxn = 50010;const Double PI = ACOs ( -1.0); ll dp[maxn][2];ll A[maxn];int Main () {int n;        while (SCANFI (n)! = EOF) {clr (dp,0);        for (int i = 1;i <= n;i++) {SCANFL (a[i]);        }//dp[1][1] = a[1];//dp[1][0] = 1;            for (int i =2; i<=n;i++) {dp[i][1] = Dp[i-1][0] + a[i]-1LL;            Dp[i][0] = dp[i-1][1] + a[i-1]-1LL;            DP[I][1] = max (dp[i-1][1] + (A[i]-a[i-1]), dp[i][1]);        Dp[i][0] = max (dp[i-1][0],dp[i][0]);    } printf ("%i64d\n", Max (dp[n][0],dp[n][1])); } return 0;}

  

Maximum cost of 51nod 1270 arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.