Codeforces 788A Functions again - 贪心
Posted 阿波罗2003
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
1 /** 2 * Codeforces 3 * Problem#788A 4 * Accepted 5 * Time:31ms 6 * Memory:400k 7 */ 8 #include<iostream> 9 #include<cstdio> 10 #include<cstdlib> 11 #include<cmath> 12 using namespace std; 13 typedef bool boolean; 14 #define inf 0x3fffffff 15 #define smin(a, b) (a) = min((a), (b)) 16 #define smax(a, b) (a) = max((a), (b)) 17 18 const long long lnf = (1LL << 61); 19 20 int n; 21 int* a; 22 long long sodd = 0, suodd = 0; 23 long long great[2][2] = {{lnf, -lnf}, {lnf, -lnf}}; //[][0] min [][1] max 24 long long res = -inf; 25 26 inline void init() { 27 scanf("%d", &n); 28 a = new int[(const int)(n + 1)]; 29 for(int i = 1; i <= n; i++) 30 scanf("%d", a + i); 31 for(int i = 1; i < n; i++) 32 a[i] = abs(a[i + 1] - a[i]); 33 } 34 35 inline void solve() { 36 if(n == 2) { 37 printf("%d\n", a[1]); 38 return; 39 } 40 great[0][0] = great[0][1] = 0; 41 for(int i = 1; i < n; i++) { 42 if(i & 1) { 43 sodd += a[i]; 44 long long c = sodd - suodd; 45 smax(res, c - great[0][0]); 46 smax(res, great[1][1] - c); 47 smax(great[1][1], c); 48 } else { 49 suodd += a[i]; 50 long long c = sodd - suodd; 51 smax(res, c - great[0][0]); 52 smax(res, great[1][1] - c); 53 smin(great[0][0], c); 54 } 55 // cout << sodd << " " << suodd << " " << res << endl; 56 } 57 printf("%I64d", res); 58 } 59 60 int main() { 61 init(); 62 solve(); 63 return 0; 64 }
以上是关于Codeforces 788A Functions again - 贪心的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 789 C Functions again DP
Codeforces Round #407 (Div. 2) C Functions again(最大子序列和)