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. NET Core 介绍

    NET Core 介绍 标签: ASP.NETCore 1. 前言 2. ASP.NET Core 简介 2.1 什么是ASP.NET Core 2.2 ASP.NET Core的特点 2.3 ASP ...

  2. Oracle EBS-SQL (INV-1):检查物料成本为0并且物料状态不是'NEW'的物料.sql

    select --msi.inventory_item_id        --,msi.organization_id     msi.segment1                        ...

  3. 在 Linux ubuntu 上安装 *.sh 文件

    简单说来就两步: 增加可执行权限 执行 事情是这样的,打算在 ubuntu 上安装一个 NetBeans IDE 来学习 Java,但是下载下来的文件是 .sh 格式的.图形界面下右键没有执行的选项. ...

  4. Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答

    Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答 Boost正则表达式库regex常用search和match示例 发表回复   Boo ...

  5. 合理的keyword密度散布与黑帽SEO之躲藏文本

    合理的keyword密度散布与黑帽SEO之躲藏文本 咱们都晓得.关于baidu的keyword排行有一个非常重要的条件即是keyword密度.在咱们的了解中keyword的密度在2%-8%这个规模之内 ...

  6. poj 3764 The xor-longest Path(字典树)

    题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...

  7. HTML5 元素拖放

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. $(this) 和 this

    在使用 jQuery 时,$(this) 和 this 具体指: this :是当前 DOM 对象: $(this) 是jQuery对象: 例子: <input type="text& ...

  9. JS 去除特定符号(逗号)的方法

    <script language="javascript"> var str="asdfk,asdf345345,345345"; //替换除数字与 ...

  10. css学习知识点

    各个前缀所代表的浏览器: Webkit: chrome, safari[也有可能是opera] Moz: 火狐 Ms: 主要是IE O: opera border-radius:  IE9  -web ...