codeforces C. Functions again
题意:给定了一个公式,让你找到一对(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的更多相关文章
- Codeforces 788A Functions again - 贪心
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian super ...
- codeforces 788A Functions again
…… 原题: Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandia ...
- CodeForces 788A - Functions again [ DP ]
反着求一遍最大连续子序列(前项依赖) #include <bits/stdc++.h> using namespace std; #define LL long long ; int n; ...
- codeforces 407 div1 A题(Functions again)
codeforces 407 div1 A题(Functions again) Something happened in Uzhlyandia again... There are riots on ...
- Codeforces E. Bash Plays with Functions(积性函数DP)
链接 codeforces 题解 结论:\(f_0(n)=2^{n的质因子个数}\)= 根据性质可知\(f_0()\)是一个积性函数 对于\(f_{r+1}()\)化一下式子 对于 \[f_{r+1} ...
- 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 ...
- 【codeforces 757E】Bash Plays with Functions
[题目链接]:http://codeforces.com/problemset/problem/757/E [题意] 给你q个询问; 每个询问包含r和n; 让你输出f[r][n]; 这里f[0][n] ...
- [Codeforces 757E] Bash Plays with Functions (数论)
题目链接: http://codeforces.com/contest/757/problem/E?csrf_token=f6c272cce871728ac1c239c34006ae90 题目: 题解 ...
- 【codeforces 789C】Functions again
[题目链接]:http://codeforces.com/contest/789/problem/C [题意] 看式子. [题解] 考虑最后的答案区间; 如果那个区间是从奇数位置的数字开始的; 那么奇 ...
随机推荐
- Bar-Code-Recognition-System Private
本设计研究EAN13条形码识别的译码技术,在基于机器视觉技术上,构建了一套条形码在线检测识别系统,系统将由以下几个部分构成:条形码图像定位与采集.计算机图像预处理.条形码图像中值滤波去噪.条码解析与识 ...
- Docker学习之安装mysql
1.从Docker检索mysql镜像 指令: docker search mysql 2.镜像下载 指令: docker pull mysql:5.7.19 3.查看本地镜像列表 指令: docker ...
- 【转】chrome devtools protocol——Web 性能自动化
前言 在测试Web页面加载时间时,可能会是这样的: 打开chrome浏览器. 按F12打开开发者工具. 在浏览器上打开要测试的页面 查看开发者工具中Network面板的页面性能数据并记录 或者在开发者 ...
- Bitmap byte[] InputStream Drawable 互转
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStrea ...
- WLC5520无法通过无线客户端进行网管故障解决
客户反馈其办公环境中的WLC5520网管需要通过内部有线网络进行管理,通过无线客户端无法进行管理,远程协助其开启WLC5520的无线管理功能后故障解决.
- 微信小程序——过滤器的模拟
>> 编写wxs文件——filter.wxs //1. 价格格式化 function getPriceFormat(value) { return parseFloat(isNaN(val ...
- 微信小程序开发小技巧——单击事件传参、动态修改样式、轮播样式修改等
一. 脚本部分: 1. 表达式无效的处理: 如果你发现自己编写的表达式无效或者数据不展示,那么请先检查你的表达式是否有添加{{}},小程序中全部都要添加的,只要是在模板中调用js中的数据 2. 获取元 ...
- golang语言中bytes包的常用函数,Reader和Buffer的使用
bytes中常用函数的使用: package main; import ( "bytes" "fmt" "unicode" ) //byte ...
- 递归函数 day17
一 递归函数 n = 1 金老板 38+2 =40n = 2 alex n+2= 金老板 36+2 = 38n = 3 wusir n+2 = alex wusir 36 def age(n): #n ...
- vue,angular,react框架对比
转载自:https://www.cnblogs.com/hubgit/p/6633214.html 首先,我们先了解什么是MVX框架模式? MVX框架模式:MVC+MVP+MVVM 1.MVC:Mod ...