C.Functions again

题意

给你一个数组a[1..n]。有一个函数\(f(l,r)=\sum_{i=l}^{r-1}\left| a[i]-a[i+1]\right| (-1)^{l-i}(1\le l< r\le n)\),求f最大值。

题解

令b[i]=|a[i]-a[i+1]|(-1)^i,c[i]=-b[i]。那么答案就是b数组和c数组的最大子序列和中较大的一个。

代码

const int N=101000;
ll n,a[N],s;
ll ans,mb,mc;
int main() {
scanf("%lld",&n);
rep(i,0,n){
scanf("%lld",a+i);
if(i){
s+=abs(a[i-1]-a[i])*(i&1?1:-1);
mc=min(-s,mc);
mb=min(s,mb);
ans=max(s-mb,ans);
ans=max(-s-mc,ans);
}
}
printf("%lld\n",ans);
return 0;
}

【cf789C】Functions again(最大子序列和)的更多相关文章

  1. CF789C. Functions again

    /* CF789C. Functions again http://codeforces.com/contest/789/problem/C 水题 题意:求数组中的连续和的最大值 */ #includ ...

  2. poj 1080 ——Human Gene Functions——————【最长公共子序列变型题】

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17805   Accepted:  ...

  3. POJ 1080 Human Gene Functions -- 动态规划(最长公共子序列)

    题目地址:http://poj.org/problem?id=1080 Description It is well known that a human gene can be considered ...

  4. Human Gene Functions POJ 1080 最长公共子序列变形

    Description It is well known that a human gene can be considered as a sequence, consisting of four n ...

  5. poj 1080 Human Gene Functions (最长公共子序列变形)

    题意:有两个代表基因序列的字符串s1和s2,在两个基因序列中通过添加"-"来使得两个序列等长:其中每对基因匹配时会形成题中图片所示匹配值,求所能得到的总的最大匹配值. 题解:这题运 ...

  6. HDU 1080 Human Gene Functions - 最长公共子序列(变形)

    传送门 题目大意: 将两个字符串对齐(只包含ACGT,可以用'-'占位),按照对齐分数表(参见题目)来计算最后的分数之和,输出最大的和. 例如:AGTGATG 和 GTTAG ,对齐后就是(为了表达对 ...

  7. Human Gene Functions

    Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18053 Accepted: 1004 ...

  8. poj1080--Human Gene Functions(dp:LCS变形)

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17206   Accepted:  ...

  9. hdu1080 Human Gene Functions() 2016-05-24 14:43 65人阅读 评论(0) 收藏

    Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. Spectral Bounds for Sparse PCA: Exact and Greedy Algorithms[贪婪算法选特征]

    目录 概括 Sparse PCA Formulation 非常普遍的问题 Optimality Conditions Eigenvalue Bounds 算法 代码 概括 这篇论文,不像以往的那些论文 ...

  2. Liunx 简单的命令说明

    cd命令在linux中用来切换或者进入目录,路径还分为相对路径和绝对路径 cd 命令:切换当前目录至其他目录 cd /:加上斜杠表示是进入到根目录. pwd命令:查看当前路径. ()cd 进入用户主目 ...

  3. js总结:增加和减少文本框

    <head><script>var count = 0; function add(){ if(count<3) { count++; var x= document.c ...

  4. 解决scrapy报错:ModuleNotFoundError: No module named 'win32api'

    ModuleNotFoundError: No module named 'win32api' 表示win32api未安装 解决办法: 下载对应python版本的win32api,并安装. 下载地址: ...

  5. nmon for Linux & Java

    nmon for Linux | Main / HomePagehttp://nmon.sourceforge.net/pmwiki.php Java Nmon Analyser download | ...

  6. PHP导出CSV文件出现乱码的解决方法

    在做项目时碰到使用外语的情况下,我们就会使用UTF-8编码.但是,在用PHP导出CSV文件时,如果写入的数据是使用UTF-8编码的日语.韩语之类的外文,就会出现乱码. 要解决PHP生成CSV文件的乱码 ...

  7. sql之cursor的简介和字符串拆分(split)与游标的使用

     字符串拆分(split)与游标的使用 CREATE TABLE Plates ( ,), ) NOT NULL, [BusinessId] INT NOT NULL, ) ),),), SELECT ...

  8. java对象的四种引用:强引用、软引用、弱引用和虚引用

    在JDK1.2之前,创建的对象只有在处于可触及(reachable)的状态下,才能被程序使用.也就是说,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.垃圾回收器一旦发现这些无用对象,就会对 ...

  9. CentOS7 下面安装jdk1.8

    1. 卸载已有的jdk rpm -qa |grep jdk |xargs rpm -e --nodeps 2. 使用xftp上传 jdk 的文件我这里上传的是 jdk-8u121-linux-x64. ...

  10. 结巴(jieba)分词

    一.介绍: jieba: “结巴”中文分词:做最好的 Python 中文分词组件 “Jieba” (Chinese for “to stutter”) Chinese text segmentatio ...