BC#86.1002(HDOJ5805)NanoApe Loves Sequence
来源:互联网 发布:linux环境变量配置 编辑:程序博客网 时间:2024/06/06 13:05
记录一个菜逼的成长。。
求出前i个数里相邻差值的最大值fi,i到n里相邻差值的最大值gi,那么ans=∑i=1nmax(∣Ai−1−Ai+1∣,fi−1,gi+1)。
时间复杂度O(n)。
一道水题。。麻痹脑子抽了写了好久。。
#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cstdlib>#include <vector>#include <set>#include <map>#include <queue>#include <list>#include <deque>#include <cctype>#include <bitset>#include <cmath>using namespace std;#define cl(a) memset(a,0,sizeof(a))typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> PII;typedef pair<LL,LL> PLL;const int INF = 0x3f3f3f3f;const int maxn = 100000 + 10;//lmx[i] 表示1-i相邻差的最大值,rmx[i]表示i-n相邻差的最大值。int a[maxn],lmx[maxn],rmx[maxn];int main(){ int T;scanf("%d",&T); while(T--){ int n; scanf("%d",&n); lmx[1] = rmx[n] = 0; for( int i = 1; i <= n; i++ ){ scanf("%d",a+i); if(i > 1){ lmx[i] = max(lmx[i-1],abs(a[i] - a[i-1])); } } for( int i = n - 1; i > 0; i-- ){ rmx[i] = max(rmx[i+1], abs(a[i+1] - a[i])); } LL ans = (LL)(lmx[n-1] + rmx[2]);//删去最左和最右两个数独立讨论 for( int i = 2; i < n; i++ ){ ans += max(abs(a[i+1] - a[i-1]),max(lmx[i-1],rmx[i+1])); } printf("%lld\n",ans); } return 0;}
0 0
- BC#86.1002(HDOJ5805)NanoApe Loves Sequence
- BC#86.1002(HDOJ5805)NanoApe Loves Sequence
- hdoj5805 NanoApe Loves Sequence && hdoj 5806 NanoApe Loves Sequence Ⅱ
- BC#86.1003(HDOJ5806)NanoApe Loves Sequence Ⅱ
- HDU 5805 NanoApe Loves Sequence (BC #86)
- 【杭电 5805 8.6 BC B NanoApe Loves Sequence】
- HDU 5805/BC 86B NanoApe Loves Sequence
- HDU 5806/BC 86C NanoApe Loves Sequence Ⅱ
- hdu NanoApe Loves Sequence
- [HDU5805]-NanoApe Loves Sequence
- hdu NanoApe Loves Sequence
- NanoApe Loves Sequence
- NanoApe Loves Sequence Ⅱ
- NanoApe Loves Sequence Ⅱ
- hdu5805-NanoApe Loves Sequence
- hdu NanoApe Loves Sequence Ⅱ
- HDU 5805 NanoApe Loves Sequence
- HDU 5805:NanoApe Loves Sequence
- Python 2-3的坑
- 文章标题
- UVA 11270 Tiling Dominoes(用1×2骨牌覆盖n×m棋盘,有多少种方法)
- 转载一篇个人觉得不错的关于对象引用的文章
- 生成Description
- BC#86.1002(HDOJ5805)NanoApe Loves Sequence
- 位运算及其应用详解
- 读书与思考
- Java 面试题
- iOS 之 cocoapods安装与使用
- 字符串反转
- 学习之路~sqh
- 二叉树遍历算法的应用
- #443 Two Sum II