[HihoCoder #1529] - Sequences that do not rise (dp + slope)

Portal

dpdpdp can be listed

f[i][j]f[i][j]f[i][j] denotes the first I I I points, with the minimum cost of j j j j

Obviously, there are f[i][j]=(min_k> =j f[i_1][k]]]+a[i]j f[i][i]a [i]]j\;f[i][j]=(\\\[k>=j}f[i-1][k]]]+|a[i]-j|f[i] [i]] [i]] [j][j]][j]]]= (min k>=j [f [i\\[i]]The result is that

Inductive method can be used to prove that f[i]f[i]f[i] is a piecewise one-time function
Consider this formula, each transfer takes a suffix Minmin and adds a piecewise one-time function with a slope of +1underline+1+1

Set the slope at 000 to ppp
Discuss the size relationship between minute a[i]a[i]a[i] and ppp

I don't want to write in detail.
You can see This

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
    char ch=gc();
    int res=0,f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
const int mod=1e9+7;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){a=add(a,b);}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=dec(a,b);}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
priority_queue<int,vector<int>,greater<int> >q;
cs int N=100005;
int n;
ll ans;
int main(){
	n=read();
	q.push(read());
	for(int i=2;i<=n;i++){
		int x=read();
		q.push(x);
		if(q.top()<x){
			ans+=x-q.top();
			q.pop(),q.push(x);
		}
	}
	cout<<ans;
}

Posted on Fri, 30 Aug 2019 21:08:32 -0700 by trex005