• Codeforces 788A Functions again


    Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:

    In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.

    Input

    The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a.

    The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements.

    Output

    Print the only integer — the maximum value of f.

    Example

    Input
    5
    1 4 2 3 1
    Output
    3
    Input
    4
    1 5 4 7
    Output
    6

    Note

    In the first sample case, the optimal value of f is reached on intervals [1, 2] and[2, 5].

    In the second case maximal value of f is reachable only on the whole array.


      这道题题目大意是说一个二元函数f(l, r),定义见前面那个式子,求最大值。

      首先呢,按照题目意思求差并取绝对值。然后得到的新的序列,求一个下标为奇数的前缀和,下标为偶数的前缀和(不是分开的!)

      例如第一个样例

           1    4    2    3    1

         差   3     2    1    2

    奇数位和   0    3    3    4    4

    偶数位和     0    0    2    2    4

     和的差   0    3    1    2    0

      下标     0    1     2    3    4

      然后用和的差就可以干很多事情了。你可以试试用下标为奇数的和的差去减之前的下标为偶数的和的差等等,然后得到的数是函数值或取相反数后就是函数值,然后可以干嘛?贪心啊!(至于这个神奇的规律的证明,展开就好了)

    Code

     1 /**
     2  * Codeforces
     3  * Problem#788A
     4  * Accepted
     5  * Time:31ms
     6  * Memory:400k
     7  */ 
     8 #include<iostream>
     9 #include<cstdio>
    10 #include<cstdlib>
    11 #include<cmath>
    12 using namespace std;
    13 typedef bool boolean;
    14 #define inf 0x3fffffff
    15 #define smin(a, b) (a) = min((a), (b))
    16 #define smax(a, b) (a) = max((a), (b))
    17 
    18 const long long lnf = (1LL << 61);
    19 
    20 int n;
    21 int* a;
    22 long long sodd = 0, suodd = 0;
    23 long long great[2][2] = {{lnf, -lnf}, {lnf, -lnf}};    //[][0]    min [][1] max
    24 long long res = -inf;
    25 
    26 inline void init() {
    27     scanf("%d", &n);
    28     a = new int[(const int)(n + 1)];
    29     for(int i = 1; i <= n; i++)
    30         scanf("%d", a + i);
    31     for(int i = 1; i < n; i++)
    32         a[i] = abs(a[i + 1] - a[i]);
    33 }
    34 
    35 inline void solve() {
    36     if(n == 2) {
    37         printf("%d
    ", a[1]);
    38         return;
    39     }
    40     great[0][0] = great[0][1] = 0;
    41     for(int i = 1; i < n; i++) {
    42         if(i & 1) { 
    43             sodd += a[i];
    44             long long c = sodd - suodd;
    45             smax(res, c - great[0][0]);
    46             smax(res, great[1][1] - c);
    47             smax(great[1][1], c);
    48         } else { 
    49             suodd += a[i];
    50             long long c = sodd - suodd;
    51             smax(res, c - great[0][0]);
    52             smax(res, great[1][1] - c);
    53             smin(great[0][0], c);
    54         }
    55 //        cout << sodd << " " << suodd << " " << res << endl;
    56     }
    57     printf("%I64d", res);
    58 }
    59 
    60 int main() {
    61     init();
    62     solve();
    63     return 0;
    64 }
  • 相关阅读:
    LaZagne — 一键抓取目标机器上的所有明文密码,todo,自己手动试试效果
    jsch文件复制(拷贝)
    使用Apache commons-pool2实现高效的FTPClient连接池的方法
    室内定位会议和期刊(2020-2021)
    Axure RP 9.0 Team Edition
    新电脑 windows 10 登录 执行此操作需要Internet
    maven配置,以及项目 Dependency xxxx not found 解决过程
    MySQL 下载网站
    Tomcat8.5和Tomcat9安装SSL证书的教程
    Python id() 函数
  • 原文地址:https://www.cnblogs.com/yyf0309/p/6847391.html
Copyright © 2020-2023  润新知