当前位置:网站首页>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;
}
边栏推荐
- Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
- C#/VB. Net merge PDF document
- Red team Chapter 8: blind guess the difficult utilization process of the package to upload vulnerabilities
- Where should older test / development programmers go? Will it be abandoned by the times?
- 动作捕捉系统用于苹果采摘机器人
- 周少剑,很少见
- 【Hot100】17. Letter combination of telephone number
- 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
- 【观察】数字化时代的咨询往何处走?软通咨询的思与行
- 数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
猜你喜欢

idea启动Command line is too long问题处理

The picgo shortcut is amazing. This person thinks exactly the same as me

IM即时通讯开发实现心跳保活遇到的问题

毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?

Pico, do you want to save or bring consumer VR?

Comprehensively view the value of enterprise digital transformation

动作捕捉系统用于苹果采摘机器人
![[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

C#/VB. Net merge PDF document

What is the digital transformation of manufacturing industry
随机推荐
Motion capture system for apple picking robot
普通二本,去过阿里外包,到现在年薪40W+的高级测试工程师,我的两年转行心酸经历...
德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测
Which MySQL functions are currently supported by tablestore in table storage?
Go 语言怎么优化重复的 if err != nil 样板代码?
【SQL语句】请问这边为什么select出了两个上海,查询出了不同的count我想让他变成一个上海,count只显示一个总和
What is the digital transformation of manufacturing industry
实现数字永生还有多久?元宇宙全息真人分身#8i
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
How does go use symmetric encryption?
Learn selenium to simulate mouse operation, and you can be lazy a little bit
数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
vim用户自动命令示例
Do280 management application deployment - pod scheduling control
ssm框架原理
FPN network details
process. env. NODE_ ENV
C#/VB. Net merge PDF document
Vscode find and replace the data of all files in a folder