当前位置:网站首页>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;
}
边栏推荐
- Golang分布式应用之etcd
- IP地址分类以及网络地址的计算(子网划分、超网划分)[通俗易懂]
- Apple bucks the trend and expands iPhone 14 series stocking, with a total of 95 million units
- 浮动及其特点
- 加密游戏:游戏的未来
- StarRocks on AWS 回顾 | Data Everywhere 系列活动深圳站圆满结束
- LeetCode 24. Swap nodes in linked list pairwise
- 面试美团被问到了Redis,搞懂这几个问题,让你轻松吊打面试官
- 「企业架构」应用架构概述
- 部署项目半途而废后续
猜你喜欢
MySQL索引的B+树到底有多高?

So delicious!Since using this interface artifact, my team efficiency has increased by 60%!

22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发

Custom filters and interceptors implement ThreadLocal thread closure

three.js blur glass effect

实践为主,理论为辅!腾讯大佬MySQL高阶宝典震撼来袭!

How to cultivate the design thinking of ui designers?

LT8911EXB MIPI CSI/DSI转EDP信号转换

郭晶晶家的象棋私教,好家伙是个机器人

mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
随机推荐
CodeForces - 628D (digital dp)
The god-level Alibaba "high concurrency" tutorial - basic + actual combat + source code + interview + architecture is all-inclusive
LeetCode 92. Reverse Linked List II
实践为主,理论为辅!腾讯大佬MySQL高阶宝典震撼来袭!
CodeForces - 628D (数位dp)
郭晶晶家的象棋私教,好家伙是个机器人
odps sql 不支持 unsupported feature CREATE TEMPORARY
人脸考勤是选择人脸比对1:1还是人脸搜索1:N?
Custom filters and interceptors implement ThreadLocal thread closure
LeetCode 21. Merge two ordered linked lists
虚拟机桥接模式不能上网
动态规划之最长回文子串
LeetCode 445. Adding Two Numbers II
CV复习:空洞卷积
啥?他一个人写了个价值100万的软件,却用来开源了!
厚积薄发!安全狗再次获得科技成果转化认证!
10 款更先进的开源命令行工具
【集合】HashSet和ArrayList的查找Contains()时间复杂度
解决 idea 单元测试不能使用Scanner
毕业总结