当前位置:网站首页>P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
2022-07-01 16:16:00 【Harris-H】
P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
一个结论就是:修改后的值一定在 原序列出现过,这样是最优的。
将 a i a_i ai 离散化递增排序得到 b b b
以递增为例子,然后就很显然令 d p ( i , j ) = m i n ( d p ( i , j ) , d p ( i − 1 , k ) + a b s ( a i − b j ) ) , k ∈ [ 1 , j ] dp(i,j)=min(dp(i,j),dp(i-1,k)+abs(a_i-b_j)),\ k \in [1,j] dp(i,j)=min(dp(i,j),dp(i−1,k)+abs(ai−bj)), k∈[1,j]
这里 m i n ( d p ( i − 1 , , k ) ) min(dp(i-1,,k)) min(dp(i−1,,k)) 可以前缀和优化。
这样复杂度: O ( n 2 ) O(n^2) O(n2)
递减同理。
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int n,m,minf[2009][2009],f[2009][2009],a[2009],b[2009],t[2009],ans;
void ini()
{
for (int i=0;i<=n;i++)
for (int j=0;j<=m;j++)
minf[i][j]=f[i][j]=0;
}
void dp()
{
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
f[i][j]=minf[i-1][j]+abs(a[i]-b[j]);
if (j!=1) minf[i][j]=min(minf[i][j-1],f[i][j]);
else minf[i][j]=f[i][j];
}
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
t[i]=a[i];
}
sort(t+1,t+1+n);
int now=-1;
for (int i=1;i<=n;i++) if (now!=t[i]) b[++m]=t[i],now=t[i];
ini(); dp(); ans=minf[n][m];
for (int i=1;i<=m/2;i++) swap(b[i],b[m-i+1]);
ini(); dp(); ans=min(ans,minf[n][m]);
printf("%d\n",ans);
return 0;
}
更优的做法是用大根堆,每次丢 a i a_i ai进堆,若比堆顶小,则把堆顶改成 a i a_i ai。
花费 ( x − y ) (x-y) (x−y)的费用,尽可能让 ( y ′ ) (y') (y′)最小,所以最优的情况就是 t o p = a i top= a_i top=ai,也就是说尽可能让前面的最大值最小,保证后面空间更大。
时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn)
// Problem: P2893 [USACO08FEB] Making the Grade G
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2893
// Memory Limit: 125 MB
// Time Limit: 3000 ms
// Date: 2022-06-30 22:45:26
// --------by Herio--------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = {
402653189,805306457,1610612741,998244353};
#define mst(a,b) memset(a,b,sizeof a)
#define db double
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr)
void Print(int *a,int n){
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
template <typename T> //x=max(x,y) x=min(x,y)
void cmx(T &x,T y){
if(x<y) x=y;
}
template <typename T>
void cmn(T &x,T y){
if(x>y) x=y;
}
int n;
ll ans=0;
int a[N];
int main(){
scanf("%d",&n);
rep(i,1,n) scanf("%d",&a[i]);
priority_queue<int>q;
rep(i,1,n){
int x = a[i];
q.push(x);
//printf("%d %d\n",x,q.top());
if(x<q.top()){
ans+=q.top()-x;
q.pop();
q.push(x);
}
}
ll s =0;
priority_queue<int,vector<int>,greater<int> >q1;
rep(i,1,n){
int x = a[i];
q1.push(x);
if(x>q1.top()){
s+=x-q1.top();
q1.pop();
q1.push(x);
}
}
//printf("%lld %lld\n",ans,s);
printf("%lld\n",min(ans,s));
return 0;
}
边栏推荐
- How to adjust the color of the computer screen and how to change the color of the computer screen
- IM即时通讯开发实现心跳保活遇到的问题
- 数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
- ssm框架原理
- Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
- Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
- ABAP call restful API
- Zhou Shaojian, rare
- Idea start command line is too long problem handling
- 【Hot100】20. 有效的括号
猜你喜欢
【Hot100】17. Letter combination of telephone number
How to adjust the color of the computer screen and how to change the color of the computer screen
She is the "HR of others" | ones character
IM即时通讯开发万人群聊消息投递方案
There will be a gap bug when the search box and button are zoomed
全面看待企业数字化转型的价值
怎麼用MySQL語言進行行列裝置?
Stonedb is building blocks for domestic databases, and the integrated real-time HTAP database based on MySQL is officially open source!
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
【Hot100】19. 删除链表的倒数第 N 个结点
随机推荐
Vscode find and replace the data of all files in a folder
How to optimize repeated if err in go language= Nil template code?
vim用户自动命令示例
Go 语言源码级调试器 Delve
UML tourism management system "suggestions collection"
全面看待企业数字化转型的价值
ssm框架原理
数据库系统原理与应用教程(004)—— MySQL 安装与配置:重置 MySQL 登录密码(windows 环境)
从大湾区“1小时生活圈”看我国智慧交通建设
Do280 management application deployment - pod scheduling control
怎么用MySQL语言进行行列装置?
Idea start command line is too long problem handling
Tutorial on principles and applications of database system (004) -- MySQL installation and configuration: resetting MySQL login password (Windows Environment)
芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
毕业后5年,我成为了年薪30w+的测试开发工程师
Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
Authentication processing in interface testing framework
Go language source level debugger delve
Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
红队第8篇:盲猜包体对上传漏洞的艰难利用过程