Codeforces 789 C Functions again (dp)

本文介绍了一道编程题的解决方案,题目要求计算Uzhlyandian函数的最大值,通过优化算法实现高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
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.


题目大意:

事情发生在Uzhlyandia…街上有暴动…著名的uzhlyandian超级商羊和STA长颈鹿被任命拯救世界。在到达后,他们发现,市民担心的主要uzhlyandian函数最大值 f,被定义为:

在上面的公式中,保证1 ≤ l < r ≤ n 其中 n 表示 Uzhlyandian 数列 a 的大小 , |x| 表示 x的绝对值。但是英雄们在学校里跳过数学课,所以他们向你寻求帮助。帮助他们计算 f 数组中的最大值, l 和 r 任选,通过给定的数组 a.


题目分析: 本来想的是开二维数组,很明显太大,然后想试试用数组的方法,就开1000*1000的,然后还不错,在第十个数据上re的,意料之中。然后就回头继续想方法,突然想到,这题很像那个最大子序列之和的,,,然后当时写完之后,居然一遍就过了样例,很是兴奋呀,后来提交wr在了第5个数据上,回头看发现是因为这个题目的每一项的正负是根据首项选谁确定的,所以是会发生改变的,然后把这个改掉之后就ac 了,少年还是需要继续加油。


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 100005;
long long a[maxn];
long long b[maxn];
int main(){
	int n;
	while((scanf("%d",&n))!=EOF){
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		for(int i=1;i<=n;i++)
		     cin>>a[i];
		for(int i=1;i<n;i++)
			b[i] = abs(a[i]-a[i+1]);
		long long l=0,mx=0;
		int flag=0;
		int k=1;
		for(int i=k;i<n;i++){
			if(flag %2 == 0 ){
				l+=b[i];
				flag++;
			}
			else {
				l-=b[i];
				flag++;
			}
			if(l>mx){
				mx=l;
			}
			if(l<0){
				l=0; flag=0;
				i--;
				i=k++;
				
			}
		}
		cout<<mx<<endl;
	}
	return 0;
}


引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值