当前位置:网站首页>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;
}
边栏推荐
- 瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
- 【Hot100】20. Valid parentheses
- Vscode find and replace the data of all files in a folder
- 如何使用phpIPAM来管理IP地址和子网
- Summer Challenge harmonyos canvas realize clock
- 【Hot100】17. 电话号码的字母组合
- 红队第8篇:盲猜包体对上传漏洞的艰难利用过程
- 毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
- The Department came to a Post-00 test paper king who took out 25K. The veteran said it was really dry, but it had been
- Endeavouros mobile hard disk installation
猜你喜欢

【Hot100】20. 有效的括号

StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!

部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...

投稿开奖丨轻量应用服务器征文活动(5月)奖励公布

There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode

The supply of chips has turned to excess, and the daily output of Chinese chips has increased to 1billion, which will make it more difficult for foreign chips

Authentication processing in interface testing framework

Im instant messaging develops a message delivery scheme for 10000 people

Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?

How long will it take to achieve digital immortality? Metacosmic holographic human avatar 8i
随机推荐
圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
Golang爬虫框架初探
VMware 虛擬機啟動時出現故障:VMware Workstation 與 Hyper-v 不兼容...
Problems encountered in IM instant messaging development to maintain heartbeat
Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项
用手机在同花顺上开户靠谱吗?这样有没有什么安全隐患
如何使用phpIPAM来管理IP地址和子网
In the past six months, it has been invested by five "giants", and this intelligent driving "dark horse" is sought after by capital
Is it reliable to open an account on flush with mobile phones? Is there any potential safety hazard
【Hot100】19. 删除链表的倒数第 N 个结点
Research on multi model architecture of ads computing power chip
Microservice tracking SQL (support Gorm query tracking under isto control)
毕业后5年,我成为了年薪30w+的测试开发工程师
What is the digital transformation of manufacturing industry
How does win11 set user permissions? Win11 method of setting user permissions
SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测
超视频时代,什么样的技术会成为底座?
The supply of chips has turned to excess, and the daily output of Chinese chips has increased to 1billion, which will make it more difficult for foreign chips