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)的更多相关文章

  1. codeforces 407 div1 B题(Weird journey)

    codeforces 407 div1 B题(Weird journey) 传送门 题意: 给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环.问满 ...

  2. 【预处理】Codeforces Round #407 (Div. 2) C. Functions again

    考虑枚举每个子串开头的位置,然后答案转化成询问这个位置之后 哪个位置的前缀和 - 这位置的前缀和 最大(当然是已经把绝对值和正负的情况处理好了,可以发现按奇偶,这个序列只有两种情况),只需要预处理这两 ...

  3. codeforces #305 div1 done

    总算搞定了这一场比赛的题目,感觉收获蛮大 其中A,B,C都能通过自己的思考解决掉 D题思路好神,E题仔细想想也能想出来 以后坚持每两天或者一天做一场CF的div1的全套题目 除非有实在无法做出来的题目 ...

  4. Educational Codeforces Round 27 补题

    题目链接:http://codeforces.com/contest/845 A. Chess Tourney 水题,排序之后判断第n个元素和n+1个元素是不是想等就可以了. #include < ...

  5. Codeforces Round #456 B题

    一.题意 给你一个n和一个k,让你从[1, n]区间内选k个数,这k个数异或和最大. 二.思路 我一开始看到这种题,不自觉地就想到,莫非又要搞很复杂的线段树.主席树?貌似还有些难搞啊.然而事实是:Co ...

  6. Codeforces数据结构(水题)小结

    最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...

  7. cordforce Educational Codeforces Round 47 补题笔记 <未完>

    题目链接 http://codeforces.com/contest/1009 A. Game Shopping 直接模拟即可,用了一个队列来存储账单 #include <iostream> ...

  8. codeforces #262 DIV2 B题 Little Dima and Equation

    题目地址:http://codeforces.com/contest/460/problem/B 这题乍一看没思路.可是细致分析下会发现,s(x)是一个从1到81的数,不管x是多少.所以能够枚举1到8 ...

  9. CodeForces 705B (训练水题)

    题目链接:http://codeforces.com/problemset/problem/705/B 题意略解: 两个人玩游戏,解数字,一个数字可以被分成两个不同或相同的数字 (3可以解成 1 2) ...

随机推荐

  1. tableView 顶部多出一部分解决方法

    1.self.automaticallyAdjustsScrollViewInsets = NO; 此方法解决之后 不能保证tableView 能彻底的滑动到底部 2. self.edgesForEx ...

  2. 自定义View(12)绘制.9图片

    代码如下: // 绘制.9图片 void draw9Path(Canvas canvas){ //创建一个ninePatch的对象实例,第一个参数是bitmap.第二个参数是byte[],这里其实要求 ...

  3. ASP.NET MVC+Bootstrap个人博客之文章打赏(六)

    看到新浪微博.百度百家等等平台上都带有文章“打赏”功能,觉得很新鲜,于是也想在自己的博客中加入“打赏”功能. 当然,加入打赏功能并非是真的想要让别人打赏.因为只有那些真正能引起共鸣,发人深思,让人受益 ...

  4. mysql之流程控制

    目录 分支结构 循环结构 分支结构: 1.if condition then [statement] elseif condition then [statement] else [statement ...

  5. webpack 报错:Module build failed: Unknown word (1:1)

    解决方法:一是确保css配置里的"style-loader"必须在"css-loader"之前,二是将整个css配置注释掉,如下图:

  6. 网盘资源搜索的一些知识 C#

    针对互联网盘上的资源进行搜索查找.写一个网盘资源搜索的要点主要有以下几点. 1.这个网盘资源搜索的原理就是利用互联网搜索引擎的site 命令. 其次就是针对网页链接进行提取以及处理. 首先就是编写一个 ...

  7. 掌握Spark机器学习库-07.6-线性回归实现房价预测

    数据集 house.csv 数据概览 代码 package org.apache.spark.examples.examplesforml import org.apache.spark.ml.fea ...

  8. ubuntu15.04安装 RVM

    首先,请参考这篇文章 https://ruby-china.org/wiki/rvm-guide RVM 官方网站 https://rvm.io/ 1 由于现在很多网站都转向https链接,所以,根据 ...

  9. jquery 移动端 六位密码输入

    <!DOCTYPE html> <html> <head> <script src="scripts/jquery-1.7.1.min.js&quo ...

  10. CAD设置背景图片(com接口)

    把图片作为背景图片可见但是不能编辑操作. 主要用到函数说明: _DMxDrawX::DrawImageToBackground 绘光栅图到背景.详细说明如下: 参数 说明 BSTR sFileName ...