codeforces 788A Functions again
……
原题:
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
2 ≤ n ≤ 105
-109 ≤ ai ≤ 109
一句话题意:
给一个数列a,求一个l和r使得上述函数最大
从i开始推dp,r==i的话f的值还和l有关,但是l对f的影响只和l的奇偶性有关
所以f[i][0]表示l在奇数,f[i][1]表示l在偶数,g[i][0]和g[i][1]表示f[1~i][0]和f[1~i][1]的最小值
然后推f,维护g,更新答案即可
注意longlong
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<ctime>
using namespace std;
const int oo=(<<)-;
int rd(){int z=,mk=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')mk=-; ch=getchar();}
while(ch>=''&&ch<=''){z=(z<<)+(z<<)+ch-''; ch=getchar();}
return z*mk;
}
int n,a[];
long long f[][],g[][];
long long mx=;
int main(){//freopen("ddd.in","r",stdin);
cin>>n;
for(int i=;i<=n;++i) a[i]=rd();
f[][]=a[]-a[];
for(int i=;i<n;++i){
f[i][]=f[i-][]+abs(a[i]-a[i+])*(i&?:-);
f[i][]=f[i-][]+abs(a[i]-a[i+])*(i&?-:);
//cout<<(a[i]-a[i+1])*(i&1?1:-1)<<" "<<(a[i]-a[i+1])*(i&1?-1:1)<<endl;
g[i][]=min(g[i-][],f[i][]);
g[i][]=min(g[i-][],f[i][]);
mx=max(mx,f[i][]-g[i][]);
mx=max(mx,f[i][]-g[i][]);
//cout<<f[i][0]<<" "<<f[i][1]<<" "<<g[i][0]<<" "<<g[i][1]<<endl;
}
cout<<mx<<endl;
return ;
}
codeforces 788A Functions again的更多相关文章
- Codeforces 788A Functions again - 贪心
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian super ...
- CodeForces 788A - Functions again [ DP ]
反着求一遍最大连续子序列(前项依赖) #include <bits/stdc++.h> using namespace std; #define LL long long ; int n; ...
- codeforces C. Functions again
题意:给定了一个公式,让你找到一对(l,r),求解出公式给定的F值. 当时没有想到,我把(-1)^(i-l)看成(-1)^i,然后思路就完全错了.其实这道题是个简单的dp+最长连续子序列. O(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 [题意] 看式子. [题解] 考虑最后的答案区间; 如果那个区间是从奇数位置的数字开始的; 那么奇 ...
随机推荐
- Windows Server 2016与旧版本系统比较
一.性能和可扩性 特征描述 Windows Server 2012/2012 R2 标准版和数据中心 Windows Server 2016 标准版和数据中心 物理内存(主机)支持 每个物理服务器至多 ...
- bzoj1798
题解: 同洛谷2023 代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; ; ll p,v,su ...
- :模板方法模式:Beverage
#ifndef __COFFINEBEVERAGE_H__ #define __COFFINEBEVERAGE_H__ #include <iostream> using namespac ...
- Cracking The Coding Interview 1.4
//Write a method to decide if two strings are anagrams or not. // // 变位词(anagrams)指的是组成两个单词的字符相同,但位置 ...
- Bluedroid: 音频数据的传输流程
一. UIPC: Audio Flinger获取到a2dp的hw module,然后蓝牙协议栈有专用于发送和接收media数据的线程,名称:btif_media_task. 蓝牙与Audio的 ...
- redis 五大数据类型之set篇
1.sadd/smembers/sismember --set集合赋值 查看值, --sismember 是查看set集合是否有指定的值,有返回1 没有返回0 2.scard,获取集合里面的元素个数 ...
- C++中数组定义及初始化
一.一维数组 静态 int array[100]; 定义了数组array,并未对数组进行初始化 静态 int array[100] = {1,2}: 定义并初始化了数组array 动态 int* ar ...
- nginx——优化 Nginx worker 进程数
Nginx 有 Master 和 worker 两种进程,Master 进程用于管理 worker 进程,worker 进程用于 Nginx 服务 worker 进程数应该设置为等于 CPU 的核数, ...
- 全局css , 样式设置, css 初始化. css ,style ,全局样式, 初始化样式
全局CSS设置总结 1.清除所有标记的内外边距 html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldse ...
- xargs用法
xargs是一个很有用的命令,它可以实现并行,同&有异曲同工之妙,在大批量管理服务器时非常有用 xargs命令是给其他命令传递参数的一个过滤器,也是组合多个命令的一个工具.它擅长将标准输入数据 ...