当前位置:网站首页>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;
}
边栏推荐
- Use Tencent cloud to build a map bed service
- What is the digital transformation of manufacturing industry
- 数据库系统原理与应用教程(002)—— MySQL 安装与配置:MySQL 软件的卸载(windows 环境)
- PR basic clip operation / video export operation
- Go 语言错误处理为什么更推荐使用 pkg/errors 三方库?
- 全面看待企业数字化转型的价值
- Is the programmer's career really short?
- Why is the pkg/errors tripartite library more recommended for go language error handling?
- [daily question] 1175 Prime permutation
- Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
猜你喜欢

运动捕捉系统原理

【Hot100】20. Valid parentheses

数据库系统原理与应用教程(002)—— MySQL 安装与配置:MySQL 软件的卸载(windows 环境)

VMware virtual machine failed during startup: VMware Workstation is incompatible with hyper-v

How to restore the system of Sony laptop

Principes et applications du système de base de données (006) - - compilation et installation de MySQL 5.7 (environnement Linux)

数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)

Germany if was crowned with many awards. How strong is this pair of headphones? In depth evaluation of yinpo GTW 270 hybrid

Research on multi model architecture of ads computing power chip

Is the programmer's career really short?
随机推荐
Advantages, values and risks of chain games compared with traditional games
Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
Principes et applications du système de base de données (006) - - compilation et installation de MySQL 5.7 (environnement Linux)
Preliminary study on golang crawler framework
巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
【每日一题】1175. 质数排列
The sharp drop in electricity consumption in Guangdong shows that the substitution of high-tech industries for high-energy consumption industries has achieved preliminary results
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
Learn selenium to simulate mouse operation, and you can be lazy a little bit
免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
Tutorial on the principle and application of database system (005) -- Yum offline installation of MySQL 5.7 (Linux Environment)
普通二本,去过阿里外包,到现在年薪40W+的高级测试工程师,我的两年转行心酸经历...
模板引擎Velocity 基礎
广东用电量大跌,说明高新技术产业替代高能耗产业已取得初步成果
你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
【Kotlin】高阶函数介绍
Vscode find and replace the data of all files in a folder
Go 语言怎么优化重复的 if err != nil 样板代码?
Tutorial on the principle and application of database system (002) -- MySQL installation and configuration: MySQL software uninstallation (Windows Environment)