1 条题解
-
0赵青海 (huhe) LV 7 SU @ 2021-8-8 1:32:41
C++ :
/***************************************** Problem Name : ******************************************/ #include <queue> #include <math.h> #include <stack> #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace std; #define LL long long const int N = 2000 + 10; const int INF = 0x3f3f3f3f; int a[N],b[N]; int dp[N][N]; int n; int f() { memcpy(b,a,sizeof(b)); sort(b+1, b+n+1); for(int i = 1 ; i <= n ; i++) { int minn = INF; for(int j = 1 ; j <= n ; j++) { minn = min(minn , dp[i-1][j]); dp[i][j] = minn + abs(a[i] - b[j]); } } int ans = INF; for(int i = 1; i <= n ; i++) { ans = min(ans , dp[n][i]); } return ans; } int main() { cin>> n; for(int i = 1 ; i <= n ; i++) cin >> a[i]; int ans = f(); reverse(a+1,a+n+1); ans = min(ans, f()); cout << ans << endl; }
- 1
信息
- ID
- 184
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 10
- 标签
- 递交数
- 6
- 已通过
- 6
- 上传者