hdu--1421--dp&&滚动数组

每个dp 没个30来分钟 tm的就写不出什么状态转移方程  还是太弱了啊=-=

        touch   me

其实用滚动数组 就是看你上一个状态转移到当前状态 与 i 前面多少个有关系  因为这里需要用到 i - 2    i - 1那么其实只要用 i % 3就可以了

像上一题 我们只涉及到 i + 1那么只需要 % 2就可以了

这只是一个 大致的方向 具体的操作 其实我们可以根据 简单的 dp转移方程来进行滚动 改变

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int size = 2010;
 7 int weight[size];
 8 int dp[size][size/2];
 9 
10 int main()
11 {
12     cin.sync_with_stdio(false);
13     int n , k;
14     while( cin >> n >> k )
15     {
16         for( int i = 1 ; i<=n ; i++ )
17         {
18             cin >> weight[i];
19         }
20         sort( weight+1 , weight+n+1 );
21         memset( dp , 0 , sizeof(dp) );
22         for( int i = 2 ; i<=n ; i++ )
23         {
24             for( int j = 1 ; j<=k ; j++ )
25             {
26                 dp[i][j] = dp[i-2][j-1] + (weight[i]-weight[i-1])*(weight[i]-weight[i-1]);
27                 if( i>j*2 )
28                     dp[i][j] = min( dp[i][j] , dp[i-1][j] );
29             }
30         }
31         cout << dp[n][k] << endl;
32     }
33     return 0;
34 }
View Code
 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int size = 2010;
 7 int weight[size];
 8 int dp[3][size/2];
 9 
10 int main()
11 {
12     cin.sync_with_stdio(false);
13     int n , k;
14     while( cin >> n >> k )
15     {
16         for( int i = 1 ; i<=n ; i++ )
17         {
18             cin >> weight[i];
19         }
20         memset( dp , 0 , 3*(k+2)*sizeof(int) );
21         sort( weight+1 , weight+n+1 );
22         for( int i = 2 ; i<=n ; i++ )
23         {
24             for( int j = 1 ; j<=k ; j++ )
25             {
26                 dp[i%3][j] = dp[(i-2)%3][j-1] + ( weight[i]-weight[i-1] )*( weight[i]-weight[i-1] );
27                 if( i>j*2 )
28                     dp[i%3][j] = min( dp[i%3][j] , dp[(i-1)%3][j] );
29             }
30         }
31         cout << dp[n%3][k] << endl;
32     }
33     return 0;
34 }
View Code

CF掉了 好多分啊啊啊啊=-=

today:

  好的时光是哪一段并无太大意义

  因为所有的时光都是被辜负被浪费的

  也只有在辜负浪费之后

  才能从记忆里将某一段拎出

  拍拍上面沉积的灰尘

  感叹它才是最好的时光

just follow your heart
原文地址:https://www.cnblogs.com/radical/p/3886631.html