首页 > 代码库 > HDU 3392 Pie(滚动数组优化)
HDU 3392 Pie(滚动数组优化)
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.
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(滚动数组优化)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。