codeforces 407 div1 A题(Functions again)
codeforces 407 div1 A题(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.
Examples
input
5
1 4 2 3 1
output
3
input
4
1 5 4 7
output
6
题解:存在两种解决方案。
#####方案1:
>如果把转化后的数列看成一个新的数列,观察可得:
>1. 相邻位的数字总是异号的
>2. 每个数字都有正负两种可能的存在
>因此可以将这个数列转化为两种形式
>+-+-+-+-.....
>-+-+-+-+.....
>我们只要对这两个数列求最大子序列和即可
方案2:
这是一个更加优美的解法,设b[i]为l为i的最大序列和,c[i]为l为i的最小序列和
b[i]=max(-c[i+1],0)+abs(a[i]-a[i+1])
c[i]=min(-b[i+1],0)+abs(a[i]-a[i+1])
方案1:
import java.io.*;
import java.util.*;
public class cf407c {
static final int N=(int)1e5+10;
static final long inf=(long)1e10;
static int a[]=new int[N];
static int b[]=new int[N];
static int c[]=new int[N];
public static void main(String[] args){
Scanner cin=new Scanner(new InputStreamReader(System.in));
while(cin.hasNext()){
int n=cin.nextInt();
for(int i =0; i<n; i++){
a[i]=cin.nextInt();
}
int tmp;
for(int i=0;i<n-1;i++){
tmp=Math.abs(a[i]-a[i+1]);
if(i%2==0){
b[i]=tmp;
c[i]=-tmp;
}else{
b[i]=-tmp;
c[i]=tmp;
}
}
long ans1=-inf,ans2=-inf;
long sum=0;
for(int i=0;i<n-1;i++){
sum+=b[i];
ans1=Math.max(ans1, sum);
if(sum<0){
sum=0;
}
}
sum=0;
for(int i=0;i<n-1;i++){
sum+=c[i];
ans2=Math.max(ans2, sum);
if(sum<0){
sum=0;
}
}
System.out.println(Math.max(ans1, ans2));
}
cin.close();
}
}
方案2:
import java.io.*;
import java.util.*;
public class cf407c {
static final int N=(int)1e5+10;
static final long inf=(long)1e10;
static int a[]=new int[N];
static long b[]=new long[N];
static long c[]=new long[N];
public static void main(String[] args){
Scanner cin=new Scanner(new InputStreamReader(System.in));
while(cin.hasNext()){
int n=cin.nextInt();
for(int i =0; i<n; i++){
a[i]=cin.nextInt();
}
b[n-1]=c[n-1]=0;
long ans=0;
for(int i=n-2;i>=0;i--){
b[i]=Math.max(-c[i+1],0)+Math.abs(a[i]-a[i+1]);
c[i]=Math.min(-b[i+1],0)+Math.abs(a[i]-a[i+1]);
ans=Math.max(ans,b[i]);
}
System.out.println(ans);
}
cin.close();
}
}
codeforces 407 div1 A题(Functions again)的更多相关文章
- codeforces 407 div1 B题(Weird journey)
codeforces 407 div1 B题(Weird journey) 传送门 题意: 给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环.问满 ...
- 【预处理】Codeforces Round #407 (Div. 2) C. Functions again
考虑枚举每个子串开头的位置,然后答案转化成询问这个位置之后 哪个位置的前缀和 - 这位置的前缀和 最大(当然是已经把绝对值和正负的情况处理好了,可以发现按奇偶,这个序列只有两种情况),只需要预处理这两 ...
- codeforces #305 div1 done
总算搞定了这一场比赛的题目,感觉收获蛮大 其中A,B,C都能通过自己的思考解决掉 D题思路好神,E题仔细想想也能想出来 以后坚持每两天或者一天做一场CF的div1的全套题目 除非有实在无法做出来的题目 ...
- Educational Codeforces Round 27 补题
题目链接:http://codeforces.com/contest/845 A. Chess Tourney 水题,排序之后判断第n个元素和n+1个元素是不是想等就可以了. #include < ...
- Codeforces Round #456 B题
一.题意 给你一个n和一个k,让你从[1, n]区间内选k个数,这k个数异或和最大. 二.思路 我一开始看到这种题,不自觉地就想到,莫非又要搞很复杂的线段树.主席树?貌似还有些难搞啊.然而事实是:Co ...
- Codeforces数据结构(水题)小结
最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...
- cordforce Educational Codeforces Round 47 补题笔记 <未完>
题目链接 http://codeforces.com/contest/1009 A. Game Shopping 直接模拟即可,用了一个队列来存储账单 #include <iostream> ...
- codeforces #262 DIV2 B题 Little Dima and Equation
题目地址:http://codeforces.com/contest/460/problem/B 这题乍一看没思路.可是细致分析下会发现,s(x)是一个从1到81的数,不管x是多少.所以能够枚举1到8 ...
- CodeForces 705B (训练水题)
题目链接:http://codeforces.com/problemset/problem/705/B 题意略解: 两个人玩游戏,解数字,一个数字可以被分成两个不同或相同的数字 (3可以解成 1 2) ...
随机推荐
- influxdb数据库增加身份认证(windows)三
接上一节,增加数据库身份认证 1.修改Config配置文件auth-enabled为true 2.然后重新载入最新的config配置文件打开数据库 3.验证身份认证功能是否已打开 说明身份认证功能已打 ...
- 第四章之S5PV210内存初始化
1,既然UART可以打印出信息来,那我们可以打印内存中的值.在506行添加如下代码: /***UART transmit function by xu ***/ display_addr_dat: l ...
- php实现rpc简单的方法
rpc是啥这不多解释,php扩展实现rpc yar是鸟哥的写的扩展,实现简单的rpc.比较很好理解 windows安装yar http://pecl.php.net/package/yar/2.0.4 ...
- Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
myeclipse 新建web项目,报错Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger,查看项目中已经有引入了 ...
- linux下的日志压缩脚本
linux下的日志压缩脚本: #!/bin/bash #第一步:先定义项目列表如下: projects="project-a project-b project-c project-d&qu ...
- Zznu 1913: yifan and matrix (多路归并)
题目链接: 1913: yifan and matrix 题目描述: 有一个n*n的矩阵,在每一行取出一个数,可以得到n个数的和,问前n小的和分别是多少? 解题思路: 对于两个数组a[n],b[n], ...
- WebSphere中数据源连接池太小导致的连接超时错误记录
WebSphere中数据源连接池太小导致的连接超时错误记录. 应用连接超时错误信息: [// ::: CST] webapp E com.ibm.ws.webcontainer.webapp.WebA ...
- Kruskal算法 分类: c/c++ 算法 2014-10-01 17:09 540人阅读 评论(0) 收藏
Kruskal算法计算最小生成树,只与边有关,时间复杂度O(eloge) 步骤: 1.将边按权值递增排序 2.依次取出边加入最小生成树中并保证无环,判断是否成环可利用并查集. 例:http://ac. ...
- 转 PHP编程过程中需要了解的this,self,parent的区别
{一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object Oriented Programming)现已经成为编程人员的一项基本技能.利用OOP的思想进行 ...
- Kali linux 2016.2(Rolling)里的应用更新和配置额外安全工具
写在前面的话 你去打人家 ,你不伪装一下,化化妆 ,穿上盔甲,难道你傻逼一样的 拿着棍子就去打人家,人家 一眼不认出你是谁了.做坏事要伪装好自己 ,要把自己藏起来 ,让别人找不到你,你以为网络公 ...