cf188C(最大子段和&&思维)
1 second
256 megabytes
standard input
standard output
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 land r for the given array a.
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.
Print the only integer — the maximum value of f.
5
1 4 2 3 1
3
4
1 5 4 7
6
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.
类似于最大循环字段和,不难发现最大子段和出现的情况分为两种:
1 +-+-+-...
2 -+-+-+...(此时一定不是从第一个数开始的所以将第一个数也置负即可)
所以只要更改+-求两次最大子段取最大值就ok
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define inf 0x7fffffff
LL a[100005];
LL LLS(int n)
{
LL sumn=0,maxn=-inf,i;
//cout<<"ssss:"<<maxn<<endl;
for(i=1;i<=n;++i){
sumn+=a[i];
maxn=max(maxn,sumn);
if(sumn<0) sumn=0;
}
return maxn;
}
int main()
{
LL n,i,j;
while(cin>>n){
for(i=1;i<=n;++i){
scanf("%lld",&a[i]);
if(i==1) continue;
a[i-1]=abs(a[i-1]-a[i]);
}
//for(i=1;i<n;++i) cout<<a[i]<<" ";
for(i=2;i<=n;i+=2) a[i]=-a[i];
j=LLS(n-1); for(i=1;i<=n-1;++i) a[i]=-a[i];
cout<<max(j,LLS(n-1))<<endl;
}
return 0;
}
cf188C(最大子段和&&思维)的更多相关文章
- 旋转子段 (思维stl)
题目: 大概意思就是给你一个序列,你可以选择一段区间使它左右翻折一遍,然后呢,从1到n找一遍,看a[i]==i的数最多是多少. 其实刚才我已经把暴力思路说出来了,枚举每一个区间长度,枚举每一个左端点, ...
- 51nod-1065 最小正子段和 【贪心 + 思维】
N个整数组成的序列a[1],a[2],a[3],-,a[n],从中选出一个子序列(a[i],a[i+1],-a[j]),使这个子序列的和>0,并且这个和是所有和>0的子序列中最小的. 例如 ...
- 假·最大子段和 (sdutoj 4359 首尾相连)(思维)
题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2736/pid/4359 具体思 ...
- 51nod 最大M子段和系列(1052、1053、1115)
51nod1052 数据量小,可使用O(N*M)的DPAC,递推公式: dp[i][j]=max(dp[i-1][j-1], dp[i][j-1])+a[j]; dp[i][j]表示前j个数取 i 段 ...
- [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序
用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html 目录 马桶排序(令人 ...
- Photoshop、Illustrator思维导图笔记
半年前学习Photoshop时记得的思维导图笔记,可能不是很全,常用的基本都记下了.
- CYQ.Data 从入门到放弃ORM系列:开篇:自动化框架编程思维
前言: 随着CYQ.Data 开始回归免费使用之后,发现用户的情绪越来越激动,为了保持这持续的激动性,让我有了开源的念头. 同时,由于框架经过这5-6年来的不断演进,以前发的早期教程已经太落后了,包括 ...
- 最大子段和(c++)
// 最大子段和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namesp ...
- 计算机程序的思维逻辑 (8) - char的真正含义
看似简单的char 通过前两节,我们应该对字符和文本的编码和乱码有了一个清晰的认识,但前两节都是与编程语言无关的,我们还是不知道怎么在程序中处理字符和文本. 本节讨论在Java中进行字符处理的基础 - ...
随机推荐
- JS实现仿腾讯微博无刷新删除微博效果代码
这里演示JS仿腾讯微博无刷新删除效果,将显示在微博列表里的内容删除,运用AJAX技术,无刷新删除微博的内容,参考性强,希望对初学AJAX的朋友有所帮助. 在线演示地址如下: http://demo.j ...
- bzoj1638 / P2883 [USACO07MAR]牛交通Cow Traffic
P2883 [USACO07MAR]牛交通Cow Traffic 对于每一条边$(u,v)$ 设入度为0的点到$u$有$f[u]$种走法 点$n$到$v$(通过反向边)有$f2[v]$种走法 显然经过 ...
- 20145105 《Java程序设计》实验五总结
实验五 JAVA网络编程 一.实验内容 用老师给的代码,实现服务器和客户端 和结对同学实现服务器与客户端的连接 客户端输入数据并加密,将密文发送给服务器,服务器接收密文并解密 二.实验步骤 用老师给的 ...
- SPOJ Prime or Not - 快速乘 - 快速幂
Given the number, you are to answer the question: "Is it prime?" Solutions to this problem ...
- C#中基于流的XML文件操作笔记
System.Xml.XmlReader和System.Xml.XmlWriters是两个抽象类,XmlReader提供了对于XML数据的快速,非缓存,只进模式的读取器,XmlWriter表示一个编写 ...
- Python3基础 os chdir 改变工作目录
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 【修改帐号信息】Eclipse中修改SVN用户名和密码方法
由于在svn 的界面中并没有为我们提供直接更换用户名密码的地方,所以一旦我们需要更换用户名的就需要自己想一些办法. 解决方案: 在Eclipse 使用SVN 的过程中大多数人往往习惯把访问SVN 的用 ...
- 【第十章】 springboot + logback
logback是boot默认的日志首选,个人觉得是最好的日志实现(比log4j好) 下边,在之前的代码基础上增加一个功能,该功能要实现mybatis的and or联查功能,并通过logback日志在控 ...
- MariaDB / MySQL数据类型
MariaDB / MySQL 数据类型 有三种主要的类型:Text(文本).Number(数字)和 Date/Time(日期/时间)类型. Text 类型: 数据类型 描述 CHAR(size) 保 ...
- socket编程时使用了inet_ntoa函数,存储到链表中的数据总是自动改变
这和inet_ntoa的返回值有关系: 函数声明:char *inet_ntoa (struct in_addr); 返回点分十进制的字符串在静态内存中的指针. 所在头文件:<arpa/inet ...