首页 技术 正文
技术 2022年11月15日
0 收藏 865 点赞 4,063 浏览 1079 个字

http://codeforces.com/contest/789/problem/C

首先按题目要求处理出dis数组。

那么对于任意一个区间,[L, R],是dis[L] – dis[L + 1] + dis[L + 2] …. +

那么怎么知道是+还是—呢?

注意到对于一个数,要么是正,要么是负。

考虑第一个数,是正的时候,第三个数肯定是取正,第二个数肯定只能是负。

那么就是对这个数组求一次最大字段和

同理第一个数是负数的情况。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e5 + ;
LL a[maxn], dis[maxn];
LL dp[maxn];
void work() {
int n;
cin >> n;
for (int i = ; i <= n; ++i) cin >> a[i];
LL ans = ;
for (int i = ; i <= n - ; ++i) {
dis[i] = abs(a[i] - a[i + ]);
}
for (int i = ; i <= n - ; i += ) {
dis[i] = -dis[i];
}
for (int i = ; i <= n - ; ++i) {
dp[i] = max(dp[i - ] + dis[i], dis[i]);
ans = max(ans, dp[i]);
}
memset(dp, , sizeof dp);
for (int i = ; i <= n - ; i += ) {
dis[i] = -dis[i];
}
for (int i = ; i <= n - ; i += ) {
dis[i] = -dis[i];
}
for (int i = ; i <= n - ; ++i) {
dp[i] = max(dp[i - ] + dis[i], dis[i]);
ans = max(ans, dp[i]);
}
cout << ans << endl;
}int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294