当前位置:网站首页>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;
}
边栏推荐
- Guide for high-end programmers to fish at work
- Action after deleting laravel's model
- Authentication processing in interface testing framework
- 部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...
- Go 语言错误处理为什么更推荐使用 pkg/errors 三方库?
- 【Hot100】19. Delete the penultimate node of the linked list
- PR basic clip operation / video export operation
- How to restore the system with one click on Lenovo laptop
- UML tourism management system "suggestions collection"
- Rhcsa Road
猜你喜欢

Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)

Dataframe gets the number of words in the string

Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)

EndeavourOS移动硬盘安装

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

巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...

芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受

In the era of super video, what kind of technology will become the base?

Activity的生命周期和启动模式详解

StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!
随机推荐
What is the digital transformation of manufacturing industry
EndeavourOS移动硬盘安装
Go 语言错误处理为什么更推荐使用 pkg/errors 三方库?
ssm框架原理
P2592 [ZJOI2008]生日聚会(dp)
【直播预约】数据库OBCP认证全面升级公开课
[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境
How to maintain the laptop battery
毕业后5年,我成为了年薪30w+的测试开发工程师
Installation and use of sqoop
How to optimize repeated if err in go language= Nil template code?
How to restore the system with one click on Lenovo laptop
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
Is it reliable to open an account on flush with mobile phones? Is there any potential safety hazard
Leetcode 216 combined summation III -- backtracking method
Basic use of MySQL
博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
Comprehensively view the value of enterprise digital transformation
【Hot100】17. Letter combination of telephone number