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. android 自定义AlertDialog

    xml: alter_dialog_two <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  2. ckeditor编辑器在java项目中配置

    一.基本使用: 1.所需文件架包 A. Ckeditor基本文件包,比如:ckeditor_3.6.2.zip 下载地址:http://ckeditor.com/download 2.配置使用 A.将 ...

  3. ubuntu qt X11开发环境

  4. Ubuntu 12.04下安装ibus中文输入法

    这是最完整的安装方法: ibu是一个框架,可以支持多种输入法,像是pinyin,五笔等. 1,安装ibus框架 终端输入以下命令: sudo apt-get install ibus ibus-clu ...

  5. VS2013启动项目调试的时候会启动本地IIS

    VS2013启动项目调试的时候会启动本地IIS ,而在这种状态下去调试程序,会有很多功能用不了,而且还会有错误:如下图: 解决方法,将托管管道模式更改一下就行了:

  6. C# 连接 Mysql 中文乱码问题

    网上有很多解决乱码的方法,什么 set names utf8   .什么在插入数据前进行编码,亲自试了都没有效果,在网上寻觅了很久,终于找到一种方法,并亲试成功: 首先要保证你的数据库是UTF8字符集 ...

  7. STL的移动算法

    要在自己定义类型中使用移动算法.须要在元素中提供移动赋值运算符.移动赋值运算符和std::move()详见<c++高级编程>第9章 class mystring { public: str ...

  8. 通过一个正则表达式,让SQL Server数据库的带参sql也支持位置参数语法!

    .NET Framework 2.0 中,Microsoft 在 System.Data.Common 名称空间下定义了一组类用来让程序员编写适用于不同数据库的数据访问代码,而且还在 Enterpri ...

  9. ES6第一篇

    //新的数字方面的方法 const I = 3.4893589; console.log(Number.parseInt(I)); console.log(Number.parseFloat(I)); ...

  10. JSP基础——关于中文乱码问题

    乱码问题十分烦人,今天在写一个登陆的demo时,表单中需要提交的中文姓名,如下代码 <form action="success.jsp" name="success ...