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) ...
随机推荐
- 实现strcat功能
实现两个字符串相连 1 #include<stdio.h> #include<string.h> //把源字符串连接到目的字符串中 char *strcat1(char *de ...
- git基本操作(入门)
下面以一个最简单的开发过程,呈现git最基本的操作命令 1.下载代码(以code命名仓库为例) git clone xxxxx/code.git cd code 2.查看所有分支 git branch ...
- Jmeter常见问题汇总(不断更新ing)
1.测试计划中有多个线程组执行时,为了防止线程组间的相互干扰,需要如下设置一下: 2,接口测试中的上传字段为汉字时需要进行什么形式的转码? 方法一:需要把编码复选框勾选,才能正常通过接口查询数 ...
- 2017 JUST Programming Contest 3.0 I. Move Between Numbers
I. Move Between Numbers time limit per test 2.0 s memory limit per test 256 MB input standard input ...
- get和post中文乱码原理相关博客
博客一: http://blog.csdn.net/saygoodbyetoyou/article/details/16834395 博客二: http://www.jb51.net/article ...
- java中什么包不需要导入
java中Math.random()*10;在math包中不需要导入: 即import java.lang.Math; 即lang下的所有包都不需要导入.
- AJPFX关于IO流的简单总结
IO流的分类:1.根据流的数据对象来分:高端流:所有的内存中的流都是高端流,比如:InputStreamReader 低端流:所有的外界设备中的流都是低端流,比如InputStream,Output ...
- 前端缓存-IndexedDB
IndexedDB 就是浏览器提供的本地数据库,它可以被网页脚本创建和操作.IndexedDB 允许储存大量数据,提供查找接口,还能建立索引.这些都是 LocalStorage 所不具备的.就数据库类 ...
- vba根据部门分别汇总不同部门下的人员不同培训内容的时长总计,多条件求和
Option Explicit Sub yy() Dim d, arr, s$, i&, m&, w$ Set d = CreateObject("Scripting.Dic ...
- 按键精灵txt判断
句子 = "度阿斯达娘阿婶是大的百度知道" 词 = "百度知道" MyPos = Instr(句子, 词) If MyPos > 0 Then Tra ...