题意:给定了一个公式,让你找到一对(l,r),求解出公式给定的F值。

  当时没有想到,我把(-1)^(i-l)看成(-1)^i,然后思路就完全错了。其实这道题是个简单的dp+最长连续子序列。

  O(n)求最长连续子序列代码

    ll maxx=, sum=, now=;
for (int i=; i<n; i++) { //数列1-n
sum+=dp1[i];
maxx=max(maxx, sum);
if (sum<) sum=;
}

  其实我们可以发现,其实正负是交错的,那么我们只要用两个dp(正负相反)的数组来存,再求一次最长连续子序列就好了。

/*  gyt
Live up to every day */
#include <stdio.h>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1e5+;
const ll maxm = 1e7;
const int mod = ;
const int INF = <<;
const db eps = 1e-;
const ll Max=1e19;
ll a[maxn], dp1[maxn], dp2[maxn]; void solve() {
int n; scanf("%d", &n);
for (int i=; i<=n; i++) {
scanf("%lld", &a[i]);
}
memset(dp1, , sizeof(dp1));
memset(dp2, , sizeof(dp2));
int f=;
for (int i=; i<=n-; i++) {
dp1[i]=abs(a[i]-a[i+])*f;
dp2[i]=abs(a[i]-a[i+])*(-f);
f = -f;
}
ll maxx=, sum=, now=;
for (int i=; i<n; i++) {
sum+=dp1[i];
maxx=max(maxx, sum);
if (sum<) sum=;
}
sum=;
for (int i=; i<n; i++) {
sum+=dp2[i];
maxx=max(maxx, sum);
if (sum<) sum=;
}
cout<<maxx<<endl;
}
int main() {
int t=;
//freopen("in.txt", "r", stdin);
//scanf("%d", &t);
for (int T=; T<=t; T++) {
solve();
}
}

最新文章

  1. git did not exit cleanly
  2. 计算机程序的思维逻辑 (50) - 剖析EnumMap
  3. [转]Ubantu vmware tools 安装
  4. win7的centos虚拟机上搭建mysql5.6服务
  5. eclipse ctrl+左击不能关联相应文件
  6. C# 光标文件的创建
  7. IOS 开展 分别制定了iphone 和 ipad 好? 或开发一个 Universal好?
  8. php前端控制器设计1
  9. git(创建,提交,回退)
  10. ORACLE聚合函数细节
  11. svn(subversion)代码版本管理在linux下的一些常见使用命令
  12. 【JS】使用变量作为object的key-方法汇总
  13. php数组实现根据某个键值将相同键值合并生成新二维数组的方法
  14. opencv人脸检测,旋转处理
  15. 在KVM里装个pfSense
  16. Ball CodeForces - 12D (线段树)
  17. HTML5学习笔记(二十八):跨域
  18. Lucene 个人领悟 (三)
  19. jd面试之感
  20. php长链接

热门文章

  1. rancher2 HA部署注意事项
  2. Python3 reversed 函数
  3. 在.NET 4中用IIS部署WCF就这么简单
  4. HDU-1176.免费馅饼(数字三角形变形)
  5. Codeforces Beta Round #46 (Div. 2)
  6. Mac安装MySQL数据库
  7. 支付宝小程序开发之与微信小程序不同的地方
  8. HDU 1166 敌兵布阵(线段树单点更新,区间查询)
  9. 浅谈Java代理二:Cglib动态代理-MethodInterceptor
  10. django的中间件:process_request|process_response|process_view|process_exception