当前位置:网站首页>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;
}
边栏推荐
- VMware 虚拟机启动时出现故障:VMware Workstation 与 Hyper-v 不兼容...
- Apple's self-developed baseband chip failed again, which shows Huawei Hisilicon's technological leadership
- [JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境
- 全面看待企业数字化转型的价值
- 數據庫系統原理與應用教程(006)—— 編譯安裝 MySQL5.7(Linux 環境)
- OJ questions related to complexity (leetcode, C language, complexity, vanishing numbers, rotating array)
- IM即时通讯开发万人群聊消息投递方案
- Uncover the "intelligence tax" of mousse: spend 4billion on marketing, and only 7 invention patents
- Submission lottery - light application server essay solicitation activity (may) award announcement
- Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
猜你喜欢

Motion capture system for apple picking robot

Where should older test / development programmers go? Will it be abandoned by the times?

SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p

广东用电量大跌,说明高新技术产业替代高能耗产业已取得初步成果

运动捕捉系统原理
![[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system](/img/04/11f24f12c52fb1f69e3d6f513d896b.png)
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system

Problems encountered in IM instant messaging development to maintain heartbeat

数据库系统原理与应用教程(004)—— MySQL 安装与配置:重置 MySQL 登录密码(windows 环境)

How to write good code - Defensive Programming Guide

EndeavourOS移动硬盘安装
随机推荐
数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
Principle of motion capture system
Where should older test / development programmers go? Will it be abandoned by the times?
Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?
学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
【SQL语句】请问这边为什么select出了两个上海,查询出了不同的count我想让他变成一个上海,count只显示一个总和
Use Tencent cloud to build a map bed service
How to adjust the size of computer photos to what you want
Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed
揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项
【Hot100】19. Delete the penultimate node of the linked list
picgo快捷键 绝了这人和我的想法 一模一样
普通二本,去过阿里外包,到现在年薪40W+的高级测试工程师,我的两年转行心酸经历...
[SQL statement] Why do you select two Shanghai and query different counts here? I want it to become a Shanghai, and count only displays a sum
FPN network details
How to use MySQL language for row and column devices?
近半年内连获5家“巨头”投资,这家智能驾驶“黑马”受资本追捧
[每日一氵]Latex 的通讯作者怎么搞
Learn selenium to simulate mouse operation, and you can be lazy a little bit