题目链接: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 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
  1. 2 3
  2. 1.5 2.0
  3. 1.5 1.7 2.0
  4. 0 0
 
Sample Output
  1. 0.000000
 
Author
momodi@whu
 
Source
 

思路: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[n][m]值,所曾经面的一些值被覆盖不影响我后面的求值过程;(能够在纸上画一画,就知道这个滚动数组了)

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <string>
  5. #include <cstdio>
  6. #include <algorithm>
  7. #include <cmath>
  8. const int maxn=11000;
  9. using namespace std;
  10.  
  11. double a[maxn],b[maxn];
  12. double dp[2][maxn];
  13.  
  14. int main()
  15. {
  16. int n,m;
  17. while(cin>>n>>m)
  18. {
  19. if(n==0&&m==0)break;
  20. for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
  21. for(int i=1;i<=m;i++)scanf("%lf",&b[i]);
  22.  
  23. double *A=a,*B=b;
  24. if(n>m){swap(n,m);swap(A,B);}
  25. sort(A+1,A+1+n);
  26. sort(B+1,B+1+m);
  27. memset(dp,0,sizeof(dp));
  28. for(int i=1;i<=n;i++)
  29. for(int j=i;j<=i+m-n;j++)
  30. {
  31. if(i==j)
  32. {
  33. dp[i&1][j]=dp[(i-1)&1][j-1]+fabs(A[i]-B[j]);
  34. //printf("dp[%d][%d] :%.6lf",i,j,dp[i&1][j]);
  35. }
  36. else
  37. {
  38. dp[i&1][j]=min(dp[(i-1)&1][j-1]+fabs(A[i]-B[j]),dp[i&1][j-1]);
  39. //printf("dp[%d][%d] :%.6lf",i,j,dp[i&1][j]);
  40. }
  41. }
  42. printf("%.6lf\n",dp[n&1][m]);
  43. }
  44. return 0;
  45. }

hdu 3392(滚动数组优化dp)的更多相关文章

  1. [BZOJ1044][HAOI2008]木棍分割 二分 + 单调队列优化dp + 滚动数组优化dp

    Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...

  2. bzoj21012101: [Usaco2010 Dec]Treasure Chest 藏宝箱(滚动数组优化dp)

    2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 592  Solved:  ...

  3. HDU_1024.MaxSumPlusPlus(基础DP + 滚动数组优化讲解)

    这道题打破了我常规的做题思路,因为这是我刚开始训练DP,感觉这道题目好晕眼呀,emm其实就是感觉自己是真的菜...... 为什么说打破了我的做题思路呢,因为我平时看题解都是在已经AC或者完全不懂的情况 ...

  4. LG3004 「USACO2010DEC」Treasure Chest 区间DP+滚动数组优化

    问题描述 LG3004 题解 把拿走的过程反向,看做添加的过程,于是很显然的区间DP模型. 设\(opt_{i,j}\)代表区间\([i,j]\)中Bessie可以获得的最大值,显然有 \[opt_{ ...

  5. dp,滚动数组优化

    51Nod1084矩阵取数问题 V2 题意: 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,先从左上走到右下,再从右下走到左上.第1遍时只能向下和向右走,第2遍时只能向上和向左 ...

  6. HDU - 1024 Max Sum Plus Plus 最大m段子段和+滚动数组优化

    给定n个数字,求其中m段的最大值(段与段之间不用连续,但是一段中要连续) 例如:2 5 1 -2 2 3 -1五个数字中选2个,选择1和2 3这两段. dp[i][j]从前j个数字中选择i段,然后根据 ...

  7. HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

    题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...

  8. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  9. 51nod 编辑距离 + 滚动数组优化

    这道题一开始觉得增加和删除会移动字符串的位置很不好做 两个字符串dp状态一般是第一个前i个和第二个前j个 #include<cstdio> #include<algorithm> ...

随机推荐

  1. [oldboy-django][2深入django]rest-framework教程

    # rest-framework教程 - settings.py INSTALL-APPS = [ 'snippets', # app 'rest-framework', ] - 创建model # ...

  2. c++ 中的slipt实现

    来自 http://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html http://blog.diveinedu.com/%E4%B8%89%E7%A7%8D%E5 ...

  3. Unity 移动方式总结

    1. 简介 在Unity3D中,有多种方式可以改变物体的坐标,实现移动的目的,其本质是每帧修改物体的position. 2. 通过Transform组件移动物体 Transform 组件用于描述物体在 ...

  4. BZOJ 1096: [ZJOI2007]仓库建设(DP+斜率优化)

    [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在 ...

  5. 【bzoj2836】魔法树 树链剖分+线段树

    题目描述 输入 输出 样例输入 4 0 1 1 2 2 3 4 Add 1 3 1 Query 0 Query 1 Query 2 样例输出 3 3 2 题解 树剖+线段树模板题,不过为什么写的人这么 ...

  6. mapserver+openlayers实现左键点击查询

    效果图 第一步,配置自己的mapfile,在要查询的图层LAYER对象内加上HEADER,TEMPLATE,FOOTER三个参数,同时,TEMPLATE fooOnlyForWMSGetFeature ...

  7. [bzoj3944] sum [杜教筛模板]

    题面: 传送门 就是让你求$ \varphi\left(i\right) $以及$ \mu\left(i\right) $的前缀和 思路: 就是杜教筛的模板 我们把套路公式拿出来: $ g\left( ...

  8. Ubuntu安装opencv with cuda

    Ubuntu安装opencv with cuda 为了运行dense flow真是折腾啊,下面网址是教程 http://blog.aicry.com/ubuntu-14-04-install-open ...

  9. iOS资讯详情页实现—WebView和TableView混合使用(转)

    iOS资讯详情页实现—WebView和TableView混合使用 如果要实现一个底部带有相关推荐和评论的资讯详情页,很自然会想到WebView和TableView嵌套使用的方案. 这个方案是WebVi ...

  10. python 集合比较(交集、并集,差集)

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...