当前位置:网站首页>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;
}
边栏推荐
- China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
- Tutorial on principles and applications of database system (006) -- compiling and installing MySQL 5.7 (Linux Environment)
- 芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
- Stegano in the world of attack and defense
- 部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...
- Pico, do you want to save or bring consumer VR?
- Apple's self-developed baseband chip failed again, which shows Huawei Hisilicon's technological leadership
- Stonedb is building blocks for domestic databases, and the integrated real-time HTAP database based on MySQL is officially open source!
- Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?
- 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
猜你喜欢
[nodemon] app crashed - waiting for file changes before starting...解决方法
从大湾区“1小时生活圈”看我国智慧交通建设
数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment
【Hot100】20. Valid parentheses
苹果自研基带芯片再次失败,说明了华为海思的技术领先性
芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
Five years after graduation, I became a test development engineer with an annual salary of 30w+
学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
Endeavouros mobile hard disk installation
随机推荐
数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
Do280 management application deployment - pod scheduling control
UML tourism management system "suggestions collection"
數據庫系統原理與應用教程(006)—— 編譯安裝 MySQL5.7(Linux 環境)
Rhcsa Road
运动捕捉系统原理
Golang爬虫框架初探
部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...
What are the differences between PHP and DW
Learn selenium to simulate mouse operation, and you can be lazy a little bit
Dataframe gets the number of words in the string
【Hot100】20. 有效的括号
OJ questions related to complexity (leetcode, C language, complexity, vanishing numbers, rotating array)
【SQL语句】请问这边为什么select出了两个上海,查询出了不同的count我想让他变成一个上海,count只显示一个总和
Go language source level debugger delve
China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
How to restore the system with one click on Lenovo laptop
实现数字永生还有多久?元宇宙全息真人分身#8i
Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?