……

原题:

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.

2 ≤ n ≤ 105

-109 ≤ ai ≤ 109

一句话题意:

给一个数列a,求一个l和r使得上述函数最大

从i开始推dp,r==i的话f的值还和l有关,但是l对f的影响只和l的奇偶性有关

所以f[i][0]表示l在奇数,f[i][1]表示l在偶数,g[i][0]和g[i][1]表示f[1~i][0]和f[1~i][1]的最小值

然后推f,维护g,更新答案即可

注意longlong

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<ctime>
  7. using namespace std;
  8. const int oo=(<<)-;
  9. int rd(){int z=,mk=; char ch=getchar();
  10. while(ch<''||ch>''){if(ch=='-')mk=-; ch=getchar();}
  11. while(ch>=''&&ch<=''){z=(z<<)+(z<<)+ch-''; ch=getchar();}
  12. return z*mk;
  13. }
  14. int n,a[];
  15. long long f[][],g[][];
  16. long long mx=;
  17. int main(){//freopen("ddd.in","r",stdin);
  18. cin>>n;
  19. for(int i=;i<=n;++i) a[i]=rd();
  20. f[][]=a[]-a[];
  21. for(int i=;i<n;++i){
  22. f[i][]=f[i-][]+abs(a[i]-a[i+])*(i&?:-);
  23. f[i][]=f[i-][]+abs(a[i]-a[i+])*(i&?-:);
  24. //cout<<(a[i]-a[i+1])*(i&1?1:-1)<<" "<<(a[i]-a[i+1])*(i&1?-1:1)<<endl;
  25. g[i][]=min(g[i-][],f[i][]);
  26. g[i][]=min(g[i-][],f[i][]);
  27. mx=max(mx,f[i][]-g[i][]);
  28. mx=max(mx,f[i][]-g[i][]);
  29. //cout<<f[i][0]<<" "<<f[i][1]<<" "<<g[i][0]<<" "<<g[i][1]<<endl;
  30. }
  31. cout<<mx<<endl;
  32. return ;
  33. }

codeforces 788A Functions again的更多相关文章

  1. Codeforces 788A Functions again - 贪心

    Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian super ...

  2. CodeForces 788A - Functions again [ DP ]

    反着求一遍最大连续子序列(前项依赖) #include <bits/stdc++.h> using namespace std; #define LL long long ; int n; ...

  3. codeforces C. Functions again

    题意:给定了一个公式,让你找到一对(l,r),求解出公式给定的F值. 当时没有想到,我把(-1)^(i-l)看成(-1)^i,然后思路就完全错了.其实这道题是个简单的dp+最长连续子序列. O(n)求 ...

  4. codeforces 407 div1 A题(Functions again)

    codeforces 407 div1 A题(Functions again) Something happened in Uzhlyandia again... There are riots on ...

  5. Codeforces E. Bash Plays with Functions(积性函数DP)

    链接 codeforces 题解 结论:\(f_0(n)=2^{n的质因子个数}\)= 根据性质可知\(f_0()\)是一个积性函数 对于\(f_{r+1}()\)化一下式子 对于 \[f_{r+1} ...

  6. Codeforces 757 E Bash Plays with Functions

    Discription Bash got tired on his journey to become the greatest Pokemon master. So he decides to ta ...

  7. 【codeforces 757E】Bash Plays with Functions

    [题目链接]:http://codeforces.com/problemset/problem/757/E [题意] 给你q个询问; 每个询问包含r和n; 让你输出f[r][n]; 这里f[0][n] ...

  8. [Codeforces 757E] Bash Plays with Functions (数论)

    题目链接: http://codeforces.com/contest/757/problem/E?csrf_token=f6c272cce871728ac1c239c34006ae90 题目: 题解 ...

  9. 【codeforces 789C】Functions again

    [题目链接]:http://codeforces.com/contest/789/problem/C [题意] 看式子. [题解] 考虑最后的答案区间; 如果那个区间是从奇数位置的数字开始的; 那么奇 ...

随机推荐

  1. 一: Docker的概念

    附件:https://files.cnblogs.com/files/chaos-li/docker-k8s-devops-master-9287a2ca56433ca076078b564de9488 ...

  2. Linux3.10.0块IO子系统流程(2)-- 构造、排序、合并请求

    Linux块设备可以分为三类.分别针对顺序访问物理设备.随机访问物理设备和逻辑设备(即“栈式设备”)   类型 make_request_fn request_fn 备注 SCSI 设备等 从bio构 ...

  3. [PyImageSearch] Ubuntu16.04 使用OpenCV和python识别信用卡 OCR

    在今天的博文中,我将演示如何使用模板匹配作为OCR的一种形式来帮助我们创建一个自动识别信用卡并从图像中提取相关信用卡数位的解决方案. 今天的博文分为三部分. 在第一部分中,我们将讨论OCR-A字体,这 ...

  4. SharePoint Framework 企业向导(六)

    博客地址:http://blog.csdn.net/FoxDave 接上一讲 部署SPFx解决方案 部署SPFx解决方案可以用两个步骤完成:1. 将脚本组件打成的包部署到一个CDN(内容分发网络) ...

  5. L252

    How often have you heard the saying, "Stop and smell the roses?" Odds are, you've come acr ...

  6. shell开源跳板机sshstack

    笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 源码地址: https://github.com/sshstack/sshstack 为什么要写shell跳板机? ...

  7. 使用Intellij Idea创建简单Maven项目(转)

    我是学Java Web的,基本靠自学,在网上收集了各种视频资料,逐一的看,代码逐一的敲.学习了这么久之前一直未成想过要把自己的学习路程记录下来,在网上也看到过很多人把自己的学习历程以及遇到的问题写在了 ...

  8. Bug04_spring注解报错

    maven 的pom文件中, 没有导入spring的依赖 <dependency> <groupId>org.springframework</groupId> & ...

  9. cygwin 解压 tar.xz压缩包

    今天第一次接触到Cygwin,啊,不懂Linux,, 解压分为三个步骤. 第一步,进入压缩包所在的文件目录: cd e:\ >(左边会弹出这个符号,我以为后面的解压要在这里写,其实不是,要再按一 ...

  10. 正则表达式的lastIndex属性

    js中正则表达式的使用方式有两种,一种是正则表达式对象的方法,一种是字符串对象的方法,前者有exec(str).test(str)两个方法,后者有match(regexp).replace(regex ...