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 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...
随机推荐
- Linux执行Cron Job失败,在Shell sh下执行却能成功 - 环境变量?
博客分类: Linux linuxcrontabpermissionetc/profile环境变量 一.我们常常碰到在shell下执行某个命令能够成功,比如执行一个java程序: java -jar ...
- 动画-缩放,旋转 CGAffineTransform
CGAffineTransform transform; // = CGAffineTransformScale(flyImoji.transform, 8, 8); transform = C ...
- go语言编程入门
查看文档 首先先分享一个可以在本地就能查看文档的骚操作(linux系统) 1.打开命令行终端,输入godoc -http=:8000,如果想后台运行在后面加个& 2.然后打开浏览器,输入网址: ...
- es新增字段,并设置默认值
重新设置mapping 添加新的字段. 设置es允许脚本执行:elasticsearch.yml script.inline: true 然后执行脚本 POST linewell_assets_mgt ...
- Tunnel Warfare--- hdu1540 线段树求连续子区间
题目链接 题意:有n个村庄,编号分别为1-n:由于战争会破坏村庄,但是我们也会修复: D x代表村庄x被破坏: Q x是求与x相连的有几个没有被破坏: R 是修复最后一次被破坏的村庄: 接下来有m个操 ...
- 洛谷P3599 Koishi Loves Construction 构造
正解:构造 解题报告: 传送门! 这题俩问嘛,就分成两个问题港QwQ 就按顺序趴,先港第一问QwQ 首先要发现,n在膜n意义下就是0嘛 那作为前缀和的话显然它就只能放在第一个 然后再想下,发现,如果n ...
- 怎样在wordpress后台显示日志 ID
有时我们需要在wordpress后台编辑特定的文章,找了大半天眼睛都花了,不禁会吐槽一下.有没有什么办法可以直接在日志列表查看日志 ID 呢?我们可以通过下面的代码实现: <?php /* Pl ...
- 004-redis-命令-哈希操作,列表操作
Redis hash 命令 下表列出了 redis hash 基本的相关命令: 序号 命令及描述 1 HDEL key field1 [field2] 删除一个或多个哈希表字段 2 HEXISTS k ...
- react-native 相关问题
使用create-react-native-app时,报错,好像是npm版本不对,想问下npm怎么降低版本? npm install npm@4 -g 创建并启动项目 老方法1 创建项目 react ...
- PHP导出excel时数字变为科学计数的解决方法
在数据导出到excel时数字格式不对,一般分为以下两种情况. 1.excel单元格设置长度不够 解决方法: //在excel.php文件中 $objActSheet = $objPHPExcel-&g ...