首页 > 代码库 > poj3666 Making the Grade

poj3666 Making the Grade

思路:

离散化+dp。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int INF = 0x3f3f3f3f;
 7 
 8 int a[2005], b[2005], dp[2005][2005], n;
 9 
10 int solve()
11 {
12     for (int i = n - 1; i >= 0; i--)
13     {
14         dp[i][n - 1] = min(INF, dp[i + 1][n - 1]) + abs(a[i] - b[n - 1]);
15         for (int j = n - 2; j >= 0; j--)
16         {
17             dp[i][j] = min(INF, dp[i][j + 1] - abs(a[i] - b[j + 1]));
18             dp[i][j] = min(dp[i][j], dp[i + 1][j]);
19             dp[i][j] += abs(a[i] - b[j]);
20         }
21     }
22     int minn = INF;
23     for (int i = 0; i < n; i++)
24     {
25         minn = min(minn, dp[0][i]);
26     }
27     return minn;
28 }
29 int main()
30 {
31     cin >> n;
32     for (int i = 0; i < n; i++)
33     {
34         scanf("%d", &a[i]);
35         b[i] = a[i];
36     }
37     int minn = INF;
38     sort(b, b + n);
39     minn = solve();
40     cout << minn << endl;
41     return 0;
42 }

总结:

1.初步学会了离散化技巧。

2.初步学会了递推优化,对完全背包的理解更深入了。

poj3666 Making the Grade