hdu 3392(滚动数组优化dp)
2014-08-22 10:43
453 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3392
Total Submission(s): 793 Accepted Submission(s): 214
Problem Description
A lot of boys and girls come to our company to pie friends. After we get their information, we need give each of them an advice for help. We know everyone’s height, and we believe that the less difference of a girl and a boy has,
the better it is. We need to find as more matches as possible, but the total difference of the matches must be minimum.
Input
The input consists of multiple test cases. The first line of each test case contains two integers, n, m (0 < n, m <= 10000), which are the number of boys and the number of girls. The next line contains n float numbers, indicating
the height of each boy. The last line of each test case contains m float numbers, indicating the height of each girl. You can assume that |n – m| <= 100 because we believe that there is no need to do with that if |n – m| > 100. All of the values of the height
are between 1.5 and 2.0.
The last case is followed by a single line containing two zeros, which means the end of the input.
Output
Output the minimum total difference of the height. Please take it with six fractional digits.
Sample Input
Sample Output
Author
momodi@whu
Source
The 5th Guangting Cup Central China Invitational
Programming Contest
思路:dp+滚动数组
(1):要求最佳匹配,首先得将两数组从小到大排序~
(2): 然后再明确dp[][]表示的意思;dp[i][j] 表示a数组中前i个数和b数组中前j个数匹配的最优解
(3):接下来 看看状态转移方程; if(i==j)dp[i][j]=dp[i-1][j-1]+fabs(a[i]-b[j]);
else dp[i][j]=min(dp[i-1][j-1]+fabs(a[i]-b[i]),dp[i][j-1]);
(4): 因为题目中的n最大取到10000,如果开个数组dp[10000][10000],那么运行不了~那么再观察观察状态转移方程,发现当前这个数是由它左边这列递推过来的,我们可以用一个dp[2][10000]的滚动数组即可,因为我只关心最后一个dp
[m]值,所以前面的一些值被覆盖不影响我后面的求值过程;(可以在纸上画一画,就知道这个滚动数组了)
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 our company to pie friends. After we get their information, we need give each of them an advice for help. We know everyone’s height, and we believe that the less difference of a girl and a boy has,
the better it is. We need to find as more matches as possible, but the total difference of the matches must be minimum.
Input
The input consists of multiple test cases. The first line of each test case contains two integers, n, m (0 < n, m <= 10000), which are the number of boys and the number of girls. The next line contains n float numbers, indicating
the height of each boy. The last line of each test case contains m float numbers, indicating the height of each girl. You can assume that |n – m| <= 100 because we believe that there is no need to do with that if |n – m| > 100. All of the values of the height
are between 1.5 and 2.0.
The last case is followed by a single line containing two zeros, which means the end of the input.
Output
Output the minimum total difference of the height. Please take it with six fractional digits.
Sample Input
2 3 1.5 2.0 1.5 1.7 2.0 0 0
Sample Output
0.000000
Author
momodi@whu
Source
The 5th Guangting Cup Central China Invitational
Programming Contest
思路:dp+滚动数组
(1):要求最佳匹配,首先得将两数组从小到大排序~
(2): 然后再明确dp[][]表示的意思;dp[i][j] 表示a数组中前i个数和b数组中前j个数匹配的最优解
(3):接下来 看看状态转移方程; if(i==j)dp[i][j]=dp[i-1][j-1]+fabs(a[i]-b[j]);
else dp[i][j]=min(dp[i-1][j-1]+fabs(a[i]-b[i]),dp[i][j-1]);
(4): 因为题目中的n最大取到10000,如果开个数组dp[10000][10000],那么运行不了~那么再观察观察状态转移方程,发现当前这个数是由它左边这列递推过来的,我们可以用一个dp[2][10000]的滚动数组即可,因为我只关心最后一个dp
[m]值,所以前面的一些值被覆盖不影响我后面的求值过程;(可以在纸上画一画,就知道这个滚动数组了)
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <cstdio> #include <algorithm> #include <cmath> const int maxn=11000; using namespace std; double a[maxn],b[maxn]; double dp[2][maxn]; int main() { int n,m; while(cin>>n>>m) { if(n==0&&m==0)break; for(int i=1;i<=n;i++)scanf("%lf",&a[i]); for(int i=1;i<=m;i++)scanf("%lf",&b[i]); double *A=a,*B=b; if(n>m){swap(n,m);swap(A,B);} sort(A+1,A+1+n); sort(B+1,B+1+m); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=i;j<=i+m-n;j++) { if(i==j) { dp[i&1][j]=dp[(i-1)&1][j-1]+fabs(A[i]-B[j]); //printf("dp[%d][%d] :%.6lf",i,j,dp[i&1][j]); } else { dp[i&1][j]=min(dp[(i-1)&1][j-1]+fabs(A[i]-B[j]),dp[i&1][j-1]); //printf("dp[%d][%d] :%.6lf",i,j,dp[i&1][j]); } } printf("%.6lf\n",dp[n&1][m]); } return 0; }
相关文章推荐
- hdu 3392 利用滚动数组优化的dp
- hdu 3392(滚动数组优化dp)
- hdu--1520--树形dp<写起来就是深搜啊>-<滚动数组优化>
- HDU 1024 Max Sum Plus Plus(普通dp && 滚动数组优化)
- HDU 4427 Math Magic【dp+优化+滚动数组】【好题】
- 【HDU 1024】Max Sum Plus Plus(DP+滚动数组优化+最大m段字段之和)
- HDU-1024-DP-(滚动数组优化与状态转移)
- HDU 3392 Pie(dp滚动数组+思路)
- HDU 4576 (2013杭州邀请赛J题-dp滚动数组优化)
- Happy Matt Friends HDU - 5119 (dp+滚动数组优化)
- hdu--1176---dp && 滚动数组优化<porker>
- HDU 1024 Max Sum Plus Plus (DP·滚动数组)
- hdu 1024 Max Sum Plus Plus 一串数字中,m段连续数字最大和 滚动数组+dp
- hdu--1421--dp&&滚动数组
- HDU - 4991(树状数组优化 dp)
- HDU 1024(动态规划+滚动数组+决策优化,求m个不相交字段和最大值)
- hdu 3450 树状数组优化dp
- [ACM] HDU 4576 Robot (概率DP,滚动数组)
- [noj 1522] 分蛋糕(dp+滚动数组优化)
- hdu 4576 Robot(dp+滚动数组)