当前位置:网站首页>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;
}
边栏推荐
- Can't global transactions be used when shardingjdbc is used in seate?
- How to use phpipam to manage IP addresses and subnets
- In the past six months, it has been invested by five "giants", and this intelligent driving "dark horse" is sought after by capital
- 【SQL语句】请问这边为什么select出了两个上海,查询出了不同的count我想让他变成一个上海,count只显示一个总和
- Im instant messaging develops a message delivery scheme for 10000 people
- Principle of SSM framework
- The picgo shortcut is amazing. This person thinks exactly the same as me
- Microservice tracking SQL (support Gorm query tracking under isto control)
- Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out
- 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
猜你喜欢
DO280管理应用部署--pod调度控制
What is the digital transformation of manufacturing industry
Embedded development: five revision control best practices
周少剑,很少见
芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
數據庫系統原理與應用教程(006)—— 編譯安裝 MySQL5.7(Linux 環境)
Malaysia's Star: Sun Yuchen is still adhering to the dream of digital economy in WTO MC12
Nuxt. JS data prefetching
Sales management system of lightweight enterprises based on PHP
VMware 虚拟机启动时出现故障:VMware Workstation 与 Hyper-v 不兼容...
随机推荐
PostgreSQL 存储结构浅析
超视频时代,什么样的技术会成为底座?
Solution to the problem that the keypad light does not light up when starting up
虚拟串口模拟器和串口调试助手使用教程「建议收藏」
2023 spring recruitment Internship - personal interview process and face-to-face experience sharing
VMware 虚拟机启动时出现故障:VMware Workstation 与 Hyper-v 不兼容...
SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测
红队第8篇:盲猜包体对上传漏洞的艰难利用过程
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
Programming examples of stm32f1 and stm32subeide - production melody of PWM driven buzzer
In the era of super video, what kind of technology will become the base?
Research on multi model architecture of ads computing power chip
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
Embedded development: five revision control best practices
When ABAP screen switching, refresh the previous screen
Principes et applications du système de base de données (006) - - compilation et installation de MySQL 5.7 (environnement Linux)
Which MySQL functions are currently supported by tablestore in table storage?
How to use phpipam to manage IP addresses and subnets
Guide for high-end programmers to fish at work