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 [题意] 看式子. [题解] 考虑最后的答案区间; 如果那个区间是从奇数位置的数字开始的; 那么奇 ...
随机推荐
- day8-python函数
函数的简介 函数就是完成特定功能的一个语句组,这组语句可以作为一个单位使用,并且给它取一个名字. 降低编程难度 代码重用 可以通过函数名在程序的不同地方多长执行,这通常叫函数调用(.). 预定义函数 ...
- Java判断对象是否为NULL
Java使用反射判断对象是否为NULL 判断Java对象是否为null可以有两层含义: 第一层: 直接使用 object == null 去判断,对象为null的时候返回true,不为null的时候 ...
- (C/C++学习笔记)附页: C/C++变量的存储类型
- 100Mbps和100MB/s
作为毕业2年计算机专业的学生,现在才知道100Mbps和100MB/s的概念,实在是渣. Mbps=Mbit/s即兆比特每秒.Million bits per second的缩写传输速率是指设备的的数 ...
- DOM&BOM
文档对象模型(Document Object Model) 来源:文档对象模型(Document Object Model)的历史与20世纪90年代末Netscape Navigator和Micros ...
- Java读取键盘输入
三种方法分别如下: 方法一:从控制台接收一个字符,然后将其打印出来 import java.io.*; public static void main(String [] args) throws I ...
- Linux:NFS配置
NFS配置 1.创建分享的文件:touch /var/www/html/aa.txt2.查看是否安装NFS:rpm -qa|grep nfs3.查看IP地址:ifconfig4.配置NFS:vi /e ...
- 关于lunece的搜索的分页和多字段搜索关键词
关于全文检索lunece的分页,我们需要用到的是以下方法 IndexSearch类下的searchAfter方法. IndexSearch isearch=new IndexSearch(a); is ...
- Python基础1--Python的安装
一.python的安装 Windows: 1) https://www.python.org/下载 2) 傻瓜式安装 3) 设置python环境变量 计算机-属性-高级系统设置-环境变量,将C:\Py ...
- Ubuntu16.04下通过tar.gz包安装MySQL5.5.52
1.下载 tar.gz包 : https://dev.mysql.com/downloads/mysql/ 2. // 安装依赖 sudo apt-get install libaio-dev // ...