当前位置:网站首页>Codeforces Round #276 (Div. 1) D. Kindergarten
Codeforces Round #276 (Div. 1) D. Kindergarten
2022-08-10 12:05:00 【Snow_raw】
Codeforces Round #276 (Div. 1) D. Kindergarten
题意: 在一个幼儿园里面,孩子被分成多个小组,每个小组必须是连续一段的,每个小组有一个魅力值,其值为该组中最大值与最小值的差,即一个人一组的小组魅力值为 0 0 0 。问如何分组可以使得所有组的魅力值之和最大?
思路: 首先有一个结论,值单调上升或者单调下降的分为一组一定是最优的。那么我么需要研究的部分即为拐点处的值应该属于上一组还是下一组?我们采用 d p dp dp 的做法,取最优值即可。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>PII;
const int N = 1e6+10;
int dp[N];
int a[N];
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int last=1;
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=2;i<=n;i++){
int x1=dp[last-1]+abs(a[i]-a[last]);
int x2=dp[last]+abs(a[i]-a[last+1]);
dp[i]=max(x1,x2);
if(i<n&&(a[i]-a[i-1])*(a[i+1]-a[i])<=0){
last=i;
}
}
cout<<dp[n]<<endl;
return 0;
}
边栏推荐
- Configure druid data source "recommended collection"
- LeetCode 21. Merge two ordered linked lists
- So delicious!Since using this interface artifact, my team efficiency has increased by 60%!
- 一文详解 implementation api embed
- LeetCode 83. Remove Duplicate Elements in Sorted List
- LeetCode 61. Rotating linked list
- MySQL相关问题整理
- Hackbar 使用教程
- LeetCode 237. Delete a node in a linked list
- iTextSharp操作PDF
猜你喜欢
随机推荐
“68道 Redis+168道 MySQL”精品面试题(带解析)
ASP.NET Core依赖注入系统学习教程:ServiceDescriptor(服务注册描述类型)
7、Instant-ngp
Guo Jingjing's personal chess teaching, the good guy is a robot
你是怎么知道数据库 Htap 能力强弱的?怎么能看出来
H264 码率控制
dedecms supports one-click import of Word content
加密游戏:游戏的未来
H264 GOP 扫盲
搜索--09
three.js blur glass effect
百度用户产品流批一体的实时数仓实践
2016,还是到了最后
日记16
【论文+代码】PEBAL/Pixel-wise Energy-biased Abstention Learning for Anomaly Segmentation on Complex Urban Driving Scenes(复杂城市驾驶场景异常分割的像素级能量偏置弃权学习)
LeetCode 86. Delimited Linked List
十八、一起学习Lua 调试(Debug)
Threshold-based filtering buffer management scheme in a shared buffer packet switch论文核心部分
You have a Doubaqiong thesaurus, please check it
AICOCO AI Frontier Promotion (8.10)









