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 }

 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 }

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

today:

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

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

  也只有在辜负浪费之后

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

  拍拍上面沉积的灰尘

  感叹它才是最好的时光

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

时间: 2024-10-28 23:12:36

hdu--1421--dp&&滚动数组的相关文章

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

hdu 3392(滚动数组优化dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3392 Pie Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 793    Accepted Submission(s): 214 Problem Description A lot of boys and girls come to ou

HDU 5617 Jam&#39;s maze dp+滚动数组

题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617 bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=666&pid=1003 题解: 设dp[x1][x2][i]表示第i步时,从(1,1)点走到了(x1,y1),(n,n)点走到了(x2,y2)点的合法的总数. 1 #include<iostream> 2 #include

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)

Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K kind of pearls. The pendant is actually a string of pearls, and its length is defined as the number of pearls in it. As is known to all, Ale

HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt's friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends'ma

poj3624 01背包入门 dp+滚动数组

poj3624 01背包 dp+滚动数组 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25458   Accepted: 11455 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the bes

POJ3071-Football(概率DP+滚动数组)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2769   Accepted: 1413 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the

HDU 3392 Pie(DP+滚动数组)

题意:有一些男生女生,男生女生数量差不超过100 ,男生女生两两配对.要求求出一种配对方法,使每一对的高度差的和最小. 思路:(我是真的笨笨笨!!磨磨唧唧写一堆是因为我笨!我看了别人的博客,思路全是学别人的,轻喷!)设人少的一组人数为n,b[],人多的一组人数为m,g[](b[],g[]先排好序),用dp[i][j]表示n中的前i个人与m中的前j个人配对所得到的最小值. 那么dp[i][j]就是min(dp[i-1][k]+|b[i]-g[k]|),就是n中前i-1个人和m中前1~k个人配对的最

HDU - 1024 Max Sum Plus Plus(dp+滚动数组优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:给定n个数字,求其中m段的最大值(段与段之间不用连续,但是一段中要连续) 例如:2 5 1 -2 2 3 -1五个数字中选2个,选择1和2 3这两段. 题解:dp[i][j]从前j个数字中选择i段,然后根据第j个数字是否独立成一段,可以写出 状态转移方程:dp[i][j]=max(dp[i][j-1]+num[j],max(dp[i-1][k])+num[j]) 这里的max(dp[i-