当前位置:网站首页>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;
}
边栏推荐
- 动作捕捉系统用于苹果采摘机器人
- 制造业数字化转型究竟是什么
- Stonedb is building blocks for domestic databases, and the integrated real-time HTAP database based on MySQL is officially open source!
- Basic use of MySQL
- Installation and use of sqoop
- Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
- How does go use symmetric encryption?
- 【Hot100】17. Letter combination of telephone number
- 【Hot100】20. Valid parentheses
- How to solve the problem that the battery icon of notebook computer does not display
猜你喜欢

投稿开奖丨轻量应用服务器征文活动(5月)奖励公布

In the past six months, it has been invested by five "giants", and this intelligent driving "dark horse" is sought after by capital

接口测试框架中的鉴权处理

數據庫系統原理與應用教程(006)—— 編譯安裝 MySQL5.7(Linux 環境)

从大湾区“1小时生活圈”看我国智慧交通建设

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

Basic use of MySQL

How to use MySQL language for row and column devices?

Rhcsa Road

sql刷题586. 订单最多的客户
随机推荐
瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
How to cancel automatic search and install device drivers for laptops
Learn selenium to simulate mouse operation, and you can be lazy a little bit
PR basic clip operation / video export operation
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
复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)
Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?
Dataframe gets the number of words in the string
Go 语言怎么优化重复的 if err != nil 样板代码?
数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
Origin2018 installation and use (sorting)
Im instant messaging develops a message delivery scheme for 10000 people
Is the programmer's career really short?
[SQL statement] Why do you select two Shanghai and query different counts here? I want it to become a Shanghai, and count only displays a sum
Principle of motion capture system
Is it reliable to open an account on flush with mobile phones? Is there any potential safety hazard
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
Buuctf gold III
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
苹果自研基带芯片再次失败,说明了华为海思的技术领先性