题意:给定了一个公式,让你找到一对(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();
}
}

codeforces C. Functions again的更多相关文章

  1. Codeforces 788A Functions again - 贪心

    Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian super ...

  2. codeforces 788A Functions again

    …… 原题: Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandia ...

  3. CodeForces 788A - Functions again [ DP ]

    反着求一遍最大连续子序列(前项依赖) #include <bits/stdc++.h> using namespace std; #define LL long long ; int n; ...

  4. codeforces 407 div1 A题(Functions again)

    codeforces 407 div1 A题(Functions again) Something happened in Uzhlyandia again... There are riots on ...

  5. Codeforces E. Bash Plays with Functions(积性函数DP)

    链接 codeforces 题解 结论:\(f_0(n)=2^{n的质因子个数}\)= 根据性质可知\(f_0()\)是一个积性函数 对于\(f_{r+1}()\)化一下式子 对于 \[f_{r+1} ...

  6. Codeforces 757 E Bash Plays with Functions

    Discription Bash got tired on his journey to become the greatest Pokemon master. So he decides to ta ...

  7. 【codeforces 757E】Bash Plays with Functions

    [题目链接]:http://codeforces.com/problemset/problem/757/E [题意] 给你q个询问; 每个询问包含r和n; 让你输出f[r][n]; 这里f[0][n] ...

  8. [Codeforces 757E] Bash Plays with Functions (数论)

    题目链接: http://codeforces.com/contest/757/problem/E?csrf_token=f6c272cce871728ac1c239c34006ae90 题目: 题解 ...

  9. 【codeforces 789C】Functions again

    [题目链接]:http://codeforces.com/contest/789/problem/C [题意] 看式子. [题解] 考虑最后的答案区间; 如果那个区间是从奇数位置的数字开始的; 那么奇 ...

随机推荐

  1. centos磁盘满了,查找大文件并清理

    今天发现vps敲入crontab -e 居然提示 “Disk quota exceeded” 无法编辑.于是"df -h"查了查发现系统磁盘空间使用100%了.最后定位到是/var ...

  2. ELK Deployed

    Enviroment prepare rpm -qa | grep java wget http://download.oracle.com/otn-pub/java/jdk/8u171-b11/51 ...

  3. hdu 1540 线段树

    这题的意思是现在有一些村庄成一条直线排列,现在有三个操作,D:摧毁一个指定的村庄,Q:询问与指定村庄相连的村庄个数, 就是这个村庄向左和向右数村庄数量,遇到尽头或损坏的村庄为止,这个就是与这个村庄相连 ...

  4. int 和 Integer 的区别

    1.两个New生成的Integer 永远不相等,因为他们的内存地址不相等 2.如果一个是New生成的Integer 另一个是通过赋值生成的话,如果值相等那么他们相等,因为这时Integer会通过自动拆 ...

  5. Android 基础 (四大组件,五大存储,六大布局)

    Android四大组件: 参考:https://blog.csdn.net/shenggaofei/article/details/52450668 Android四大组件分别为activity.se ...

  6. Shell教程 之流程控制

    1. if else 1.1 if if语句语法格式: if condition then command1 command2 ... commandN fi 写成一行(适用于终端命令提示符): if ...

  7. 学习Auxre记录

    1.下载地址:https://www.axure.com.cn/3510/(可以免费试用30天) 2.视频教程:https://huke88.com/course/6455.html 3.文字教程:h ...

  8. Unity性能优化 – 脚本篇

    https://wuzhiwei.net/unity_script_optimization/

  9. crm作业知识点集合[三]

    知识点1 我们要实现一个这样的功能,在学生表中,可以查看每个学生的报名的班级的所有的成绩,就是下图的效果 1.首先我们需要在学生表中自定义一列,这一列的内容就是一个a标签,指向另外一个页面,而我们在另 ...

  10. ubuntu下安装redis及常用操作

    reids是一个内存数据库,具有存取速度快,使用简单等优点.我们常常在分布式或者负载均衡的系统中使用它来缓存一些公用的且不是很大量的数据,比如session或者各类token(比如微信的access_ ...