Pie

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 895    Accepted Submission(s): 246

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
 

题解:

输入n个男生,m个女生的身高。

把人数较少的一方和另外一方匹配完,求最少的差值。

分别把男女的身高排序。人数较少的一方的每个人可以匹配abs(m-n+1)个人。

因为|m-n|<=100 所以一个人最多匹配100人。

之后用二维滚动数组就可以了。

dp[i][j]男对女相错j个人的最小身高差;

dp[i%2][k]=min(dp[(i-1)%2][k]+abs(a[i]-b[j]),dp[i%2][k-1])

不知道为啥sort一直错qsort就对了。。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x) scanf("%lf",&x)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=;
double dp[][],a[MAXN],b[MAXN];
int cmp(const void *a,const void *b)
{
return *(double *)a < *(double *)b ? : -;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m),n||m){
if(n<=m){
for(int i=;i<=n;i++)SD(a[i]);
for(int i=;i<=m;i++)SD(b[i]);
}
else{
swap(n,m);
for(int i=;i<=m;i++)SD(b[i]);
for(int i=;i<=n;i++)SD(a[i]);
}
qsort(a+,n,sizeof(a[]),cmp);
qsort(b+,m,sizeof(b[]),cmp);
int len=m-n+;
mem(dp,);
for(int i=;i<=n;i++){
dp[i%][]=dp[(i-)%][]+abs(a[i]-b[i]);
for(int k=;k<=len;k++){
int j=i+k-;
dp[i%][k]=min(dp[(i-)%][k]+abs(a[i]-b[j]),dp[i%][k-]);
}
}
printf("%.6lf\n",dp[n%][len]);
}
return ;
}

Pie(求最小身高差,dp)的更多相关文章

  1. cell_phone_network(树形dp求最小支配集)

    Cell Phone Network Farmer John has decided to give each of his cows a cell phone in hopes to encoura ...

  2. 北京师范大学第十六届程序设计竞赛决赛 C萌萌哒身高差

    链接:https://www.nowcoder.com/acm/contest/117/C来源:牛客网 萌萌哒身高差 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...

  3. bnuoj-53073 萌萌哒身高差 【数学】【非原创】

    "清明时节雨纷纷,路上行人欲断魂." 然而wfy同学的心情是愉快的,因为BNU ACM队出去春游啦!并且,嗯... 以下是wfy同学的日记: 昨天,何老师告诉我们:明天我们去春游, ...

  4. SQL Server 2008 R2——查找最小nIndex,nIndex存在而nIndex+1不存在 求最小连续数组中的最大值

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  请通过右侧公告中的“联系邮 ...

  5. hiho 第116周,最大流最小割定理,求最小割集S,T

    小Hi:在上一周的Hiho一下中我们初步讲解了网络流的概念以及常规解法,小Ho你还记得内容么? 小Ho:我记得!网络流就是给定了一张图G=(V,E),以及源点s和汇点t.每一条边e(u,v)具有容量c ...

  6. poj 3469 Dual Core CPU【求最小割容量】

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 21453   Accepted: 9297 ...

  7. BZOJ_1001_狼抓兔子_(平面图求最小割+对偶图求最短路)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1001 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec   ...

  8. The Minimum Length - HUST 1010(求最小循环节)

    题意:有个一字符串A(本身不是循环串),然后经过很多次自增变成AAAAA,然后呢从自增串里面切出来一部分串B,用这个串B求出来A的长度.   分析:其实就是求最小循环节.......串的长度 - 最大 ...

  9. [KMP求最小循环节][HDU1358][Period]

    题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...

随机推荐

  1. eclipse android sdk javadoc

    sdk 的函数不提示帮助信息 查了下是现在adt版本没有doc文件夹,拷贝了早期的版本docs过来 其他具体操作如下:http://blog.csdn.net/lyh7736362/article/d ...

  2. Oracle EBS-SQL (WIP-3):检查非标任务子件没选MRP净值.sql

    SELECT WE.WIP_ENTITY_NAME,           MSI.SEGMENT1,           MSI.DESCRIPTION,           WDJ.CLASS_CO ...

  3. ps -aux返回超过100%

    http://serverfault.com/questions/522922/cpu-more-than-100-in-ps-aux export NLS_LANG="SIMPLIFIED ...

  4. react-native学习笔记——简单尝试

    毫无疑问,我是个不善于写博文的人. 毫无疑问,react是个出的框架. 毫无疑问,react-native更是个牛逼的引擎. 我个人对react-native的理解就是js被js引擎编译,去调用本地语 ...

  5. Developer‘s提升开发效率的工具和插件或编程语言

    1.Git 之前也有过不少版本控制的工具.有好的,也有糟糕的.不过它们都或多或少地误入歧途了. 这时候Git出现了.一旦你用上了这个神奇的工具,很难相像你还会碰到比它更好的了. 还没用过Git?试一下 ...

  6. why constrained regression and Regularized regression equivalent

    problem 1: $\min_{\beta} ~f_\alpha(\beta):=\frac{1}{2}\Vert y-X\beta\Vert^2 +\alpha\Vert \beta\Vert$ ...

  7. NULL和nullptr的区别

    //error C2665: “go”: 2 个重载中没有一个可以转换所有参数类型 #include <iostream> void go(int num) { std::cout < ...

  8. 【玩转Ubuntu】01. Ubuntu上配置JDK

    一.安装JDK 提示:这里我们使用jdk1.6,因为android开发要求使用1.6.如果不信你可以打开android studio,它会提示你选择JDK6的路径 下载地址: http://www.o ...

  9. bootstarp(carousel)组件

    ##### 1.5.1.Bootstrap中轮播图插件叫作Carousel ##### 1.5.2.基本的轮播图实现 ```html <!-- 以下容器就是整个轮播图组件的整体, 注意该盒子必须 ...

  10. jQuery源码笔记——延迟对象

    提供一种方法来执行一个或多个对象的回调函数, Deferred对象通常表示异步事件. 它是回调对象的拓展运用,在jQuery当中非常依赖回调对象. 一个简单的,只解决成功状态下的缓存实例 functi ...