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. git 文件回滚

    场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file.场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第 ...

  2. VK Cup 2018 - Round 1 A. Primal Sport

    A. Primal Sport time limit per test 1.5 seconds memory limit per test 256 megabytes input standard i ...

  3. Plugging an Unplugged Pluggable Database issue 3

    Multitenant Unplug/Plug Best Practices (文档 ID 1935365.1) 1.source 从0419 升级到1019 ,但是datapatch 没有回退041 ...

  4. 472 Concatenated Words 连接的单词

    详见:https://leetcode.com/problems/concatenated-words/description/ C++: class Solution { public: vecto ...

  5. 对于JS == 运算的一些理解

    声明:本文是摘自一篇文章,放在这只为做为一个笔记能更好学习. 大家知道,==是JavaScript中比较复杂的一个运算符.它的运算规则奇怪,容易让人犯错,从而成为JavaScript中“最糟糕的特性” ...

  6. ReactJS-2-props vs state

    rops理解: 大多数组件都可以在创建的时候被不同的参数定制化,这些不同的参数就叫做props.props的流向是父组件到子组件. 子组件Comment,是一条评论组件,父组件CommentList, ...

  7. Objective-C Memory Management Being Exceptional 异常处理与内存

    Objective-C Memory Management    Being Exceptional  异常处理与内存 3.1Cocoa requires that all exceptions mu ...

  8. Windows下使用JMeter

    简介 Apache JMeter是100%纯java桌面应用程序,被设计用来测试C/S结构的软件(例如web应用程序).它可以被用来测试包括基于静态和动态资源程序的性能,例如静态文件,Java Ser ...

  9. OC 实现一个TODO宏

    实现一个TODO宏 转载http://blog.sunnyxx.com/2015/03/01/todo-macro/ 实现一个能产生warning的TODO宏,用于在代码里做备忘,效果: 下面一步步来 ...

  10. Win7系统32位和64位的区别

    Win7系统32位和64位的区别已经是一个老话题了,可是还是有很多朋友不明白.这两者到底有什么区别呢?下面本文与大家通俗的介绍下Win7系统32位和64位的区别,其他一些深入的理论讲述,大家可以看看文 ...