codeforces C. Functions again
2024-09-26 09:37:29
题意:给定了一个公式,让你找到一对(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();
}
}
最新文章
- git did not exit cleanly
- 计算机程序的思维逻辑 (50) - 剖析EnumMap
- [转]Ubantu vmware tools 安装
- win7的centos虚拟机上搭建mysql5.6服务
- eclipse ctrl+左击不能关联相应文件
- C# 光标文件的创建
- IOS 开展 分别制定了iphone 和 ipad 好? 或开发一个 Universal好?
- php前端控制器设计1
- git(创建,提交,回退)
- ORACLE聚合函数细节
- svn(subversion)代码版本管理在linux下的一些常见使用命令
- 【JS】使用变量作为object的key-方法汇总
- php数组实现根据某个键值将相同键值合并生成新二维数组的方法
- opencv人脸检测,旋转处理
- 在KVM里装个pfSense
- Ball CodeForces - 12D (线段树)
- HTML5学习笔记(二十八):跨域
- Lucene 个人领悟 (三)
- jd面试之感
- php长链接
热门文章
- rancher2 HA部署注意事项
- Python3 reversed 函数
- 在.NET 4中用IIS部署WCF就这么简单
- HDU-1176.免费馅饼(数字三角形变形)
- Codeforces Beta Round #46 (Div. 2)
- Mac安装MySQL数据库
- 支付宝小程序开发之与微信小程序不同的地方
- HDU 1166 敌兵布阵(线段树单点更新,区间查询)
- 浅谈Java代理二:Cglib动态代理-MethodInterceptor
- django的中间件:process_request|process_response|process_view|process_exception