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. vb combobox 用法问题总结

    问题一 combobox 通过type类型,如下代码,通过选取name名称(改变combobox的名称)得到 其Id Type User id As Integer userName As Strin ...

  2. 使用ASP.NET MVC+Entity Framework快速搭建博客系统

    学习 ASP.NET MVC 也有一段时间了,打算弄个小程序练练手,做为学习过程中的记录和分享. 首先,得确定需求,木有需求的话,那还搞个毛线呀!嗯……大致思考了一下,终于得出如下需求: 1.能自定义 ...

  3. [Node.js]expressjs简单测试连接mysql

    下载好node.js和通过npm安装好express.js后,先写package.json { "name": "application-name", &quo ...

  4. Hibernate的Criteria用法

    在hibernate的Session里面使用createCriteria可以创建一个Criteria实例帮助我们进行条件查询,不用自己串hql语句,很方便. 用法很简单,首先传Class实例创建Cri ...

  5. SNMP_802.1

    http://www.cisco.com/en/US/tech/tk648/tk362/technologies_tech_note09186a00801c9199.shtml http://www. ...

  6. InstallShield 工程类型MSI 使用过程中碰到的问题及解决方法。

    原文 http://www.cnblogs.com/daocaorenbx/p/InstallShield.html 一.安装完 桌面创建IE快捷方式: 1.  install script可以直接建 ...

  7. xshell使用命令总结

    这个工具主要是链接linux 并且可以从linux上面下载文件到本地 还有上传本地文件到linux上面 下载首先需要压缩打包命令为: tar -cf am_mailer.tar * sz am_mai ...

  8. [LeetCode] Rotate Image [26]

    题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...

  9. sql2008中时间类型问题

    DATEDIFF (DD ,@sdate ,getdate() ) eg30 计算从开始日期到今天的天数 datename(weekday,@sdate) eg星期三 查询那一天是星期几 SQL Se ...

  10. javascript高级知识点——内置对象原型

    代码信息来自于http://ejohn.org/apps/learn/. 可以修改内置对象的方法. if (!Array.prototype.forEach) { Array.prototype.fo ...