码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3392 Pie(滚动数组优化)

时间:2014-10-07 13:51:13      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   io   os   ar   for   sp   div   

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<cmath>
typedef long long LL;
using namespace std;
const int maxn=10000+100;
double A[maxn],G[maxn];
double dp[2][maxn];
int n,m;
int main()
{
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        if(n>m)  swap(n,m);
        for(int i=1;i<=n;i++)
            scanf("%lf",&A[i]);
        for(int i=1;i<=m;i++)
            scanf("%lf",&G[i]);
        double *b=A,*g=G;
//        if(n>m)   {swap(n,m);swap(b,g);}
        memset(dp,0,sizeof(dp));
        sort(b+1,b+n+1);
        sort(g+1,g+m+1);
        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(b[i]-g[j]);
                else
                   dp[i&1][j]=min(dp[(i-1)&1][j-1]+fabs(b[i]-g[j]),dp[i&1][j-1]);
            }
        }
        printf("%.6f\n",dp[n&1][m]);
    }
    return 0;
}
/*
2 5
1.0 2.0
0.7 1.5 1.5 1.8 2.8
5 2
0.7 1.5 1.5 1.8 2.8
1.0 2.0
*/

HDU 3392 Pie(滚动数组优化)

标签:des   style   color   io   os   ar   for   sp   div   

原文地址:http://blog.csdn.net/u013582254/article/details/39852679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!