hdu 4723 How Long Do You Have to Draw(贪心)
2015-07-27 23:33
459 查看
How Long Do You Have to Draw
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 277 Accepted Submission(s): 110
Problem Description
There are two horizontal lines on the XoY plane. One is y1 = a, the other is y2 = b(a < b). On line y1, there are N points from left to right, the x-coordinate of which are x = c1, c2, ... , cN (c1 <
c2 < ... < cN) respectively. And there are also M points on line y2 from left to right. The x-coordinate of the M points are x = d1, d2, ... dM (d1 < d2 < ... < dM)
respectively.
Now you can draw segments between the points on y1 and y2 by some segments. Each segment should exactly connect one point on y1 with one point on y2.
The segments cannot cross with each other. By doing so, these segments, along with y1 and y2, can form some triangles, which have positive areas and have no segments inside them.
The problem is, to get as much triangles as possible, what is the minimum sum of the length of these segments you draw?
Input
The first line has a number T (T <= 20) , indicating the number of test cases.
For each test case, first line has two numbers a and b (0 <= a, b <= 104), which is the position of y1 and y2.
The second line has two numbers N and M (1 <= N, M <= 105), which is the number of points on y1 and y2.
The third line has N numbers c1, c2, .... , cN(0 <= ci < ci+1 <= 106), which is the x-coordinate of the N points on line y1.
The fourth line has M numbers d1, d2, ... , dM(0 <= di < di+1 <= 106), which is the x-coordinate of the M points on line y2.
Output
For test case X, output "Case #X: " first, then output one number, rounded to 0.01, as the minimum total length of the segments you draw.
Sample Input
1 0 1 2 3 1 3 0 2 4
Sample Output
Case #1: 5.66
Source
2013 ACM/ICPC Asia Regional Online —— Warmup2
Recommend
zhuyuanchen520
题意:两条平行线,各有n、m个点。要连一些线,两个端点分别是两条平行线上的点,并且不能交叉。在取得最多三角形的情况下,求最小的总的线的长度。
题解:要保证有最多的三角形 得把所有的点都连上。这样先把两平行线的最左端两点先连上,再依次把两条平行线最左端没连的选距离小的连上。
#include<cstring> #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> #define N 100010 using namespace std; double a ,b ; int main() { //freopen("test.in","r",stdin); int t; cin>>t; int ca=1; while(t--) { double y1,y2; scanf("%lf%lf",&y1,&y2); int n,m; scanf("%d%d",&n,&m); for(int i=0; i<n; i++) { scanf("%lf",&a[i]); } for(int j=0; j<m; j++) { scanf("%lf",&b[j]); } sort(a,a+n); sort(b,b+m); printf("Case #%d: ",ca++); if(n==1&&m==1) { printf("0.00\n"); continue; } if(n==0||m==0) { printf("0.00\n"); continue; } double ans=sqrt((y1-y2)*(y1-y2)+(a[0]-b[0])*(a[0]-b[0])); int l1=1,l2=1; while(l1<n&&l2<m) { double dis1=(y1-y2)*(y1-y2)+(a[l1]-b[l2-1])*(a[l1]-b[l2-1]); double dis2=(y1-y2)*(y1-y2)+(a[l1-1]-b[l2])*(a[l1-1]-b[l2]); if(dis1<dis2) { ans+=sqrt(dis1); l1++; } else { ans+=sqrt(dis2); l2++; } } while(l1<n) { double dis1=(y1-y2)*(y1-y2)+(a[l1]-b[l2-1])*(a[l1]-b[l2-1]); ans+=sqrt(dis1); l1++; } while(l2<m) { double dis2=(y1-y2)*(y1-y2)+(a[l1-1]-b[l2])*(a[l1-1]-b[l2]); ans+=sqrt(dis2); l2++; } printf("%.2f\n",ans ); } return 0; }
相关文章推荐
- [2015-07-26] python008
- Python应用 ---- Mac环境下Flask+Nginx+FastCGI实现Python应用部署
- [黑马程序员](第37天)今晚敲的一些代码题
- Android Studio 卡在building界面
- Spring <context:annotation-config/> 解说
- 选择器
- iterator、const_iterator和const iterator的区别
- 【Android Studio初探】快捷键大全与常用设置
- (转)Android 中的 Service 全面总结
- HDU 3746 Cyclic Nacklace 环形项链(KMP,循环节)
- [LeetCode] Different Ways to Add Parentheses
- View类详解
- 程序连接haproxy线程一直close_wait问题
- 简单实用H5的FormData对象上传文件
- POJ-2585-Mayor's posters-成段更新+(hash)离散化思想
- EXCEL表格常用函数使用的难点
- WorldWind源码剖析系列:表面影像类SurfaceImage
- Activity生命周期及启动模式深入理解
- 似大地水准面精化
- letcode - Dungeon Game