当前位置:网站首页>273. 分级 - AcWing题库【DP】
273. 分级 - AcWing题库【DP】
2022-07-17 20:46:00 【Codiplay】
总结:
- 对于给定的A序列,第一维肯定是枚举前i个数
- 第二维,初步想法是以j结尾的最小值,j的范围是1e9范围过大
- 利用贪心的思路,可以得到:存在一种构造序列B的方案使得所有的B都在A中出现过。
- 可以将第二维的j变成一个有限的集合即A集合,从而范围缩小了
- brute force即可,注意到k值的搜寻只进不出用minv维护,减去一维for(k)
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2100;
int f[N][N], a[N], b[N];
int n;
//对于前i个A,以Bj为结尾最小值
//子问题:找到前i-1个A,以Bk为结尾的转移过来 k <= j
int work()
{
for (int i = 1; i <= n; i ++ ) {
int minv = INF;
for (int j = 1; j <= n; j ++ ) {
minv = min(minv, f[i - 1][j]);
f[i][j] = minv + abs(b[j] - a[i]);
}
}
int ans = INF;
for (int i = 1; i <= n; i ++ ) {
ans = min(ans, f[n][i]);
}
return ans;
}
int main(){
//std::ios::sync_with_stdio(false);
//std::cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
b[i] = a[i];
}
sort(b + 1, b + n + 1);
int ans = work();
reverse(a, a + n);
ans = min(ans, work());
return 0;
}边栏推荐
- O'Neill's RPS curve compilation method (original by Dr. Tao)
- 2022年中国AI医学影像行业概览报告
- Redis源码与设计剖析 -- 1.简单动态字符串
- 数据库的增删改查
- Initial understanding of functions - next
- 「津津樂道播客」#392 原湯話原食:仲夏夜,馬砂、肉串兒、趿拉板兒
- Unity subtitle scrolling
- unity 字幕滚动
- Redis源码与设计剖析 -- 3.字典
- [C language] introduction and implementation of mine sweeping [array and function]
猜你喜欢
![[acwing] solution of the 60th weekly match](/img/79/5cc097c7a432e40c4bda3ef5a167de.gif)
[acwing] solution of the 60th weekly match

4 a company has branches in six cities C1, C2, C3... C6. The connection between cities Ci and CJ (I, j=1,2,3,... 6) and the cost are listed in the following weighted adjacency matrix C

ImportError: DLL load failed while importing win32api: 找不到指定的程序。

NO.2汇编初步

「津津樂道播客」#392 原湯話原食:仲夏夜,馬砂、肉串兒、趿拉板兒

BiShe - online reservation and registration system based on SSM

全面解析C语言多媒体开源框架GStreamer

面额10exp(303)的钞票

iVX低代码平台系列详解 -- 概述篇(二)

NO.6浮点数的表示与运算
随机推荐
[acwing] solution of the 60th weekly match
【7.12】Codeforces Round #806 (Div. 4)
数据库的增删改查
2022年中国AI医学影像行业概览报告
陶博士月线反转6.0
Huawei technologies:jonathan Krolikowski | from design to deployment, zero contact deep reinforcement learning WLANs
No.4 bits, bytes, information storage
BiShe - online reservation and registration system based on SSM
Silent AI: how does shengteng AI solve the problem of sign language learning with large models?
Luo Gu: p3092 [usaco13nov]no change G
FreeRTOS个人笔记-支持多优先级
主流erp系统排名,主流erp系统对比
STL string replication comparison
华为无线设备配置静态负载均衡
Luogu p3512 [poi2010] PIL pilots problem solution
No.3 advanced assembly
新建一个eureka client
Brief introduction of Bezier curve
版本通告|Apache Doris 1.1 Release 版本正式发布!
贝塞尔曲线简单介绍