当前位置:网站首页>P2893 [usaco08feb] making the grade g (DP & priority queue)
P2893 [usaco08feb] making the grade g (DP & priority queue)
2022-07-01 16:39:00 【Harris-H】
P2893 [USACO08FEB] Making the Grade G(dp& Priority queue )
One conclusion is : The modified value must be The original sequence has appeared , This is the best .
take a i a_i ai Discretized incremental sorting results in b b b
Take incremental as an example , Then it's obvious that 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]
here m i n ( d p ( i − 1 , , k ) ) min(dp(i-1,,k)) min(dp(i−1,,k)) You can prefix and optimize .
This complexity : O ( n 2 ) O(n^2) O(n2)
The same goes for diminishing .
#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 better approach is to use a large root pile , Every time a i a_i ai In pile , If it is smaller than the top of the pile , Change the top of the pile to a i a_i ai.
cost ( x − y ) (x-y) (x−y) The cost of , As far as possible let ( y ′ ) (y') (y′) Minimum , So the best case is t o p = a i top= a_i top=ai, In other words, try to minimize the previous maximum , Make sure there is more space behind .
Time complexity : 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;
}
边栏推荐
- Défaillance lors du démarrage de la machine virtuelle VMware: le poste de travail VMware n'est pas compatible avec hyper - V...
- Leetcode 216 combined summation III -- backtracking method
- Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- 【观察】数字化时代的咨询往何处走?软通咨询的思与行
- 动作捕捉系统用于苹果采摘机器人
- Principle of SSM framework
- 【直播预约】数据库OBCP认证全面升级公开课
- 怎么用MySQL语言进行行列装置?
- sql刷题627. 变更性别
猜你喜欢

VMware 虚拟机启动时出现故障:VMware Workstation 与 Hyper-v 不兼容...

怎么用MySQL语言进行行列装置?
![[nodemon] app crashed - waiting for file changes before starting...解决方法](/img/ee/9830afd86e092851a2a906cb994949.png)
[nodemon] app crashed - waiting for file changes before starting...解决方法

Motion capture system for apple picking robot

独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作

游戏行业安全选择游戏盾,效果怎么样?

China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District

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

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

数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
随机推荐
Zabbix2.2监控之系统及应用日志监控报警
高端程序员上班摸鱼指南
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
FPN network details
数据库系统原理与应用教程(002)—— MySQL 安装与配置:MySQL 软件的卸载(windows 环境)
PR basic clip operation / video export operation
Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
I'm a senior test engineer who has been outsourced by Alibaba and now has an annual salary of 40w+. My two-year career changing experience is sad
Example of vim user automatic command
Golang爬虫框架初探
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
Principle of motion capture system
How to use phpipam to manage IP addresses and subnets
为国产数据库添砖加瓦,StoneDB 一体化实时 HTAP 数据库正式开源!
P2592 [ZJOI2008]生日聚会(dp)
How does go use symmetric encryption?
如何使用phpIPAM来管理IP地址和子网
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
How to repair the laptop that cannot connect to the wireless network