Dancing Cows 训练赛题目--低级匹配(暴力) [SPOJ-DCOWS]
来源:互联网 发布:陈翔6点半之网络大电影 编辑:程序博客网 时间:2024/06/02 02:50
题目链接
Dancing Cows SPOJ - DCOWS
It’s the spring dance and, in a rare occurrence, the N (1 ≤ N ≤ 5000) bulls have been invited to dance with the M (N < M ≤ 5000) cows (as long as they stay on their very best behavior).
Farmer John, almost obsessive-compulsive in his organization of dances, wants the spectacle to be as visually attractive as possible. Thus, he wants to pair up the N bulls with cow partners such that the total of all the magnitudes of differences in height is minimized. Bulls have heights B_i (1 ≤ B_i ≤ 1,000,000) and cows have height C_i (1 ≤ C_i ≤ 1,000,000). Of course, some cows will be unmatched since N-M of them will have no partners; ignore their heights.
INPUT FORMAT:
- Line 1: Two space-separated integers: N and M.
- Lines 2..N+1: Line i+1 contains a single integer: B_i.
Lines N+2..M+N+1: Line i+N+1 contains a single integer: C_i.
OUTPUT FORMAT:Line 1: A single integer that is the minimum of the sum of the absolute value of the height differences that can be achieved.
SAMPLE INPUT :
5 7
10
16
12
10
13
7
17
12
10
9
6
11
SAMPLE OUTPUT :
4
INPUT DETAILS:
Five bulls + seven cows with various heights:
Bulls: 10 10 12 13 16
Cows: 6 7 9 10 11 12 17
OUTPUT DETAILS :
Here is one way to achieve a total difference of 4:
Bulls: 10 10 12 13 16
Cows: 6 7 9 10 11 12 17
题意:n只公牛,m只母牛,使公牛和母牛完成匹配,使得匹配的母牛和公牛其差值的绝对值之和最小
思路:dfs暴力匹配
code:
样例
5 710 10 12 13 166 7 9 10 11 12 17
b: 0 c: 0b: 1 c: 1b: 2 c: 2b: 3 c: 3b: 4 c: 4b: 5 c: 50 16 11 5b: 4 c: 5b: 5 c: 60 16 12 4b: 4 c: 6b: 5 c: 70 16 17 1b: 4 c: 7b: 4 c: 7*b: 4 c: 6*b: 4 c: 5*1 13 10 3b: 3 c: 4b: 4 c: 51 13 11 2b: 3 c: 5b: 4 c: 61 13 12 1b: 3 c: 6b: 4 c: 71061109567 13 17 4b: 3 c: 7b: 3 c: 7*b: 3 c: 6*b: 3 c: 5*b: 3 c: 4*2 12 9 3b: 2 c: 3b: 3 c: 42 12 10 2b: 2 c: 4b: 3 c: 52 12 11 1b: 2 c: 5b: 3 c: 61061109567 12 12 0b: 2 c: 6b: 3 c: 71061109567 12 17 5b: 2 c: 7b: 2 c: 7*b: 2 c: 6*b: 2 c: 5*b: 2 c: 4*b: 2 c: 3*3 10 7 3b: 1 c: 2b: 2 c: 33 10 9 1b: 1 c: 3b: 2 c: 43 10 10 0b: 1 c: 4b: 2 c: 51061109567 10 11 1b: 1 c: 5b: 2 c: 61061109567 10 12 2b: 1 c: 6b: 2 c: 71061109567 10 17 7b: 1 c: 7b: 1 c: 7*b: 1 c: 6*b: 1 c: 5*b: 1 c: 4*b: 1 c: 3*b: 1 c: 2*3 10 6 4b: 0 c: 1b: 1 c: 23 10 7 3b: 0 c: 2b: 1 c: 33 10 9 1b: 0 c: 3b: 1 c: 41061109567 10 10 0b: 0 c: 4b: 1 c: 51061109567 10 11 1b: 0 c: 5b: 1 c: 61061109567 10 12 2b: 0 c: 6b: 1 c: 71061109567 10 17 7b: 0 c: 7b: 0 c: 7*b: 0 c: 6*b: 0 c: 5*b: 0 c: 4*b: 0 c: 3*b: 0 c: 2*b: 0 c: 1*4
理解代码
#include<cstdio>#include<cstring>#include<cstdlib>#include<bits/stdc++.h>using namespace std;const int maxn = 6000;const int inf = 0x3f3f3f3f;long long dp[maxn][maxn];long long bi[maxn],ci[maxn];long long n,m;long long cal(int b,int c){ printf("b: %d c: %d\n",b,c); if(b==n) return 0; if(c==m) return inf; if(dp[b][c]!=-1) return dp[b][c]; long long ret = cal(b+1,c+1); printf("%lld %lld %lld %lld\n",ret,bi[b],ci[c],abs(bi[b]-ci[c])); ret +=abs(bi[b]-ci[c]); long long temp = cal(b,c+1); ret = min(ret,temp); cout<<endl; return dp[b][c] = ret;}int main(){ memset(dp,-1,sizeof(dp)); scanf("%lld%lld",&n,&m); for(int i=0;i<n;i++) scanf("%lld",&bi[i]); for(int i=0;i<m;i++) { scanf("%d",&ci[i]); } sort(bi,bi+n); sort(ci,ci+m); printf("%lld\n",cal(0,0));}
code2(AC): 不要额外开很大的空间,会导致超时
#include<cstdio>#include<cstring>#include<cstdlib>#include<bits/stdc++.h>using namespace std;const int maxn = 5010;const long long inf = 1e17;long long dp[maxn][maxn];long long bi[maxn],ci[maxn];long long n,m;long long cal(int b,int c){ //printf("b: %d c: %d\n",b,c); if(b==n) return 0; if(c==m) return inf; if(dp[b][c]!=-1) return dp[b][c]; long long ret = cal(b+1,c+1)+abs(bi[b]-ci[c]); // printf("%lld %lld %lld %lld\n",ret,bi[b],ci[c],abs(bi[b]-ci[c])); //long long temp = cal(b,c+1); ret = min(ret,cal(b,c+1)); // cout<<endl; return dp[b][c] = ret;}int main(){ memset(dp,-1,sizeof(dp)); scanf("%lld%lld",&n,&m); for(int i=0;i<n;i++) scanf("%lld",&bi[i]); for(int i=0;i<m;i++) { scanf("%lld",&ci[i]); } sort(bi,bi+n); sort(ci,ci+m); printf("%lld\n",cal(0,0));}
dp:
#include<bits/stdc++.h>using namespace std;long long dp[5001][5001],sel;const long long maxn = 1e16;int main(){ int n,m; scanf("%d %d",&n,&m); int bulls[5001]= {0},cows[5001]= {0}; for(int i=1; i<=n; i++) scanf("%d",&bulls[i]); for(int i=1; i<=m; i++) scanf("%d",&cows[i]); sort(bulls,bulls+n+1); sort(cows,cows+m+1); for(int i=0; i<=m; i++) dp[0][i]=0; for(int i=1; i<=n; i++) dp[i][0]=maxn; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { dp[i][j]=min(dp[i][j-1],dp[i-1][j-1]+abs(bulls[i]-cows[j])); } } printf("%lld\n",dp[n][m]); return 0;}
- Dancing Cows 训练赛题目--低级匹配(暴力) [SPOJ-DCOWS]
- 字符串匹配(暴力匹配)
- HDU 5533 Dancing Stars on Me (暴力模拟+思维)
- USACO 1.2 Milking Cows(暴力枚举)
- SPOJ 1771 Yet Another N-Queen Problem 解题报告(Dancing Link)
- Spoj 1771(Dancing Links 精确覆盖变形)
- POJ 题目3621 Sightseeing Cows(SPFA)
- POJ 题目2481 Cows(树状数组)
- CodeForces - 63C Bulls and Cows(暴力求解)
- Brute Force匹配(暴力匹配)和FLANN匹配区别
- 算法训练 字串统计 (暴力)
- 编程训练题目(转)
- SPOJ AE00 Rectangles 【基础】【暴力】
- Cutting (暴力 + 滚动哈希判字符串匹配)
- HDOJ 题目1172猜数字(暴力)
- NYOJ 题目495少年 DXH(暴力)
- HDOJ 题目1287 破译密码(暴力)
- NOJ 题目1643 阶乘除法(暴力)
- 本地FTP 使用。
- Java 之 文件读写及性能比较总结
- c++之共用体、结构体占用内存
- erlang wx图形界面学习-4
- PowerDesigner使用教程 —— 概念数据模型(CDM模型)
- Dancing Cows 训练赛题目--低级匹配(暴力) [SPOJ-DCOWS]
- 11月英语
- FileUtil
- Educational Codeforces Round 31 F. Anti-Palindromize
- c++需要注意unsigned与signed区别
- Selenium 元素定位 CSS and XPath
- springboot 依赖管理
- DllImport属性
- toString()方法