首页 > 代码库 > 把一个序列转换成非严格递增序列的最小花费 POJ 3666

把一个序列转换成非严格递增序列的最小花费 POJ 3666

 1 //把一个序列转换成非严格递增序列的最小花费 POJ 3666 2 //dp[i][j]:把第i个数转成第j小的数,最小花费 3  4 #include <iostream> 5 #include <cstdio> 6 #include <cstdlib> 7 #include <algorithm> 8 #include <vector> 9 #include <math.h>10 // #include <memory.h>11 using namespace std;12 #define LL long long13 typedef pair<int,int> pii;14 const int inf = 0x3f3f3f3f;15 const LL MOD =100000000LL;16 const int N = 3000+10;17 const double eps = 1e-8;18 void fre() {freopen("in.txt","r",stdin);}19 void freout() {freopen("out.txt","w",stdout);}20 inline int read() {int x=0,f=1;char ch=getchar();while(ch>9||ch<0) {if(ch==-) f=-1; ch=getchar();}while(ch>=0&&ch<=9) {x=x*10+ch-0;ch=getchar();}return x*f;}21 22 int a[N],b[N];23 LL dp[N][N];24 int main(){25     int n;26     scanf("%d",&n);27     for(int i=1;i<=n;i++){28         scanf("%d",&a[i]);29         b[i]=a[i];30     }31     sort(b+1,b+1+n);32     for(int i=1;i<=n;i++){33         dp[1][i]=abs(a[1]-b[i]);34     }35     for(int i=2;i<=n;i++){36         LL minn=1e18;37         for(int j=1;j<=n;j++){38            minn=min(minn,dp[i-1][j]);39            dp[i][j]=minn+abs(a[i]-b[j]);40         }41     }42     LL ans=1e18;43     for(int i=1;i<=n;i++){44         ans=min(ans,dp[n][i]);45     }46     cout<<ans<<endl;47     return 0;48 }

 

把一个序列转换成非严格递增序列的最小花费 POJ 3666