C - Sequence CodeForces - 13C
题目链接:http://codeforces.com/problemset/problem/13/C
Little Petya likes to play very much. And most of all he likes to play the following game:
He is given a sequence of N integer numbers. At each step it is allowed to increase the value of any number by 1 or to decrease it by 1. The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math, so he asks for your help.
The sequence a is called non-decreasing if a1 ≤ a2 ≤ ... ≤ aN holds, where N is the length of the sequence.
Input
The first line of the input contains single integer N (1 ≤ N ≤ 5000) — the length of the initial sequence. The following N lines contain one integer each — elements of the sequence. These numbers do not exceed 109 by absolute value.
Output
Output one integer — minimum number of steps required to achieve the goal.
Examples
5
3 2 -1 2 11
4
5
2 1 1 1 1
1
题目大意:大概的题意就是每次可以对一个数加一或者减一,然后用最小的代价使得原序列变成不下降的序列。
思路:自己最开始的思路是dp[i][j] 第i个数刚好为第j大,并且前i个数满足不递减的关系的最少步数,但是这样想的话要用三重for 所以就超时了 因为我们要枚举从上一个数的所有大小转变过来
这就加了一维了。
正解:dp[i][j]表示前i个树不大于j 并且满足不递减的关系的最少步数
因为是不大于j 所以dp[i][j]可以等于dp[i][j-1]和dp[i-1][j]+abs(a[i]-a[j]) 这样就只有二维了
看代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<stack> #include<map> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<stack> #include<map> #include<queue> #include<cmath> using namespace std; typedef long long LL; typedef unsigned long long ull; #define sc1(a) scanf("%lld",&a) #define pf1(a) printf("%lld\n",a) #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 const LL INF=1e18; const ull base=2333; const int maxn=5e3+5; const int maxm=1e3+50; const int maxv=1e6+5; const int mod=1e9+7; const int ba=3e5; LL N; LL a[maxn],b[maxn]; /** dp[i][j]表示前i个数最大的数不大于第j大的数 并且前i个数单调不递减的最小步数 */ LL dp[5][maxn]; int main() { sc1(N); for(int i=1;i<=N;i++) { sc1(a[i]); b[i]=a[i]; } sort(b+1,b+1+N);//从小到大 for(int i=0;i<=1;i++) { for(int j=0;j<=N;j++) dp[i][j]=INF; } for(int i=1;i<=N;i++) { dp[0][i]=0; } for(int i=1;i<=N;i++) { int v=i%2; int w=(i-1)%2; for(int j=0;j<=N;j++) dp[v][j]=INF; for(int j=1;j<=N;j++) { dp[v][j]=min(dp[v][j-1],dp[w][j]+abs(a[i]-b[j])); } } LL ans=INF; int v=N%2; for(int i=1;i<=N;i++) ans=min(ans,dp[v][i]); pf1(ans); return 0; } /** */