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.
Input
The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements.
Output
Print the only integer — the maximum value of f.
Example
5
1 4 2 3 1
3
4
1 5 4 7
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and[2, 5].
In the second case maximal value of f is reachable only on the whole array.
这道题题目大意是说一个二元函数f(l, r),定义见前面那个式子,求最大值。
首先呢,按照题目意思求差并取绝对值。然后得到的新的序列,求一个下标为奇数的前缀和,下标为偶数的前缀和(不是分开的!)
例如第一个样例
1 4 2 3 1
差 3 2 1 2
奇数位和 0 3 3 4 4
偶数位和 0 0 2 2 4
和的差 0 3 1 2 0
下标 0 1 2 3 4
然后用和的差就可以干很多事情了。你可以试试用下标为奇数的和的差去减之前的下标为偶数的和的差等等,然后得到的数是函数值或取相反数后就是函数值,然后可以干嘛?贪心啊!(至于这个神奇的规律的证明,展开就好了)
Code
/**
* Codeforces
* Problem#788A
* Accepted
* Time:31ms
* Memory:400k
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef bool boolean;
#define inf 0x3fffffff
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b)) const long long lnf = (1LL << ); int n;
int* a;
long long sodd = , suodd = ;
long long great[][] = {{lnf, -lnf}, {lnf, -lnf}}; //[][0] min [][1] max
long long res = -inf; inline void init() {
scanf("%d", &n);
a = new int[(const int)(n + )];
for(int i = ; i <= n; i++)
scanf("%d", a + i);
for(int i = ; i < n; i++)
a[i] = abs(a[i + ] - a[i]);
} inline void solve() {
if(n == ) {
printf("%d\n", a[]);
return;
}
great[][] = great[][] = ;
for(int i = ; i < n; i++) {
if(i & ) {
sodd += a[i];
long long c = sodd - suodd;
smax(res, c - great[][]);
smax(res, great[][] - c);
smax(great[][], c);
} else {
suodd += a[i];
long long c = sodd - suodd;
smax(res, c - great[][]);
smax(res, great[][] - c);
smin(great[][], c);
}
// cout << sodd << " " << suodd << " " << res << endl;
}
printf("%I64d", res);
} int main() {
init();
solve();
return ;
}
Codeforces 788A Functions again - 贪心的更多相关文章
- 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 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- CodeForces - 50A Domino piling (贪心+递归)
CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...
- Codeforces 161 B. Discounts (贪心)
题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...
- CodeForces 176A Trading Business 贪心
Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a ...
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...
- Codeforces 486C Palindrome Transformation(贪心)
题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...
- Codeforces 1154D - Walking Robot - [贪心]
题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...
随机推荐
- SecureFX 中文乱码
1. 找到SecureFX配置文件夹(选项–全局选项,常规下的配置文件夹),比如:C:\Users\James\AppData\Roaming\VanDyke\Config\Sessions 2. ...
- python MD5步骤
https://www.cnblogs.com/zipon/p/8340720.html import hashlib def get_token(): md5str = "abc" ...
- paas平台
paas平台 定义:PaaS是云计算中重要的一类服务,为用户提供应用的全生命周期管理和相关的资源服务.通过PaaS,用户可以完成应用的构建.部署.运维管理,而不需要自己去搭建计算环境,如安装服务器.操 ...
- curl 一个无比有用的网站开发工具
1.Common Line Url Viewer curl是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在"标准输出"(stdout)上面. 2.-i参数可以显示h ...
- SQL Server 登录名、服务器角色、用户名和数据库角色 --- 解释
一.基本解释 登录名:登录服务器的用户账号: 服务器角色:登录名对该服务器具有的权限,角色分多种的,一个角色可以有多个登录名,如操作系统的系统用户可以有多个. SQL服务器角色 sysadmin ...
- 请用漂亮欢呼-------Day38
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/marSmile_tbo/article/details/31108557 周末,双休,疯了两天.敲了 ...
- HTTP API响应数据规范整理
概述 本文档为本人对长期开发API接口所整理的经验总结,如有不完善或不合理的地方,望各位多提意见. 文档目的为规范服务器端API接口,便于服务器端与客户端代码重用.服务器端和客户端可根据实际所定义规范 ...
- windows server r2 之如何设置共享文件夹访问不需要输入用户名和密码
第一步: 打开guest账号.单击桌面“开始”按钮,找到“控制面板”并打开,选择“用户帐户”并单击就会弹出一个窗口,继续单击下方的“管理其他帐户”,然后选择“Guest”,点击“启用”. 第二步: 在 ...
- checkbox选择
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 机器学习理论基础学习3.4--- Linear classification 线性分类之Gaussian Discriminant Analysis高斯判别模型
一.什么是高斯判别模型? 二.怎么求解参数?