hdoj-4723-How Long Do You Have to Draw
2016-05-04 22:34
543 查看
Description
There are two horizontal lines on the XoY plane. One is y 1 = a, the other is y 2 = b(a < b). On line y 1, there are N points from left to right, the x-coordinate of which are x = c 1, c 2, … , c N (c 1 < c 2 < … < c N) respectively. And there are also M points on line y 2 from left to right. The x-coordinate of the M points are x = d 1, d 2, … d M (d 1 < d 2 < … < d M) respectively.
Now you can draw segments between the points on y 1 and y 2 by some segments. Each segment should exactly connect one point on y 1 with one point on y 2.
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 <= 10 4), which is the position of y 1 and y 2.
The second line has two numbers N and M (1 <= N, M <= 10 5), which is the number of points on y 1 and y 2.
The third line has N numbers c 1, c 2, …. , c N(0 <= c i < c i+1 <= 10 6), which is the x-coordinate of the N points on line y 1.
The fourth line has M numbers d 1, d 2, … , d M(0 <= d i < d i+1 <= 10 6), which is the x-coordinate of the M points on line y 2.
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
贪心题吧,外带一点数学知识。
两条线段,两堆点。然后找尽量多的三角形,求最多的三角形的时候连接线段的最小长度,两个边不相交。从左往右,记l和r为两条平行线第一个没被选的点。计算选l或选r新增的线的长度,选最小的。画个图就一目了然了。
There are two horizontal lines on the XoY plane. One is y 1 = a, the other is y 2 = b(a < b). On line y 1, there are N points from left to right, the x-coordinate of which are x = c 1, c 2, … , c N (c 1 < c 2 < … < c N) respectively. And there are also M points on line y 2 from left to right. The x-coordinate of the M points are x = d 1, d 2, … d M (d 1 < d 2 < … < d M) respectively.
Now you can draw segments between the points on y 1 and y 2 by some segments. Each segment should exactly connect one point on y 1 with one point on y 2.
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 <= 10 4), which is the position of y 1 and y 2.
The second line has two numbers N and M (1 <= N, M <= 10 5), which is the number of points on y 1 and y 2.
The third line has N numbers c 1, c 2, …. , c N(0 <= c i < c i+1 <= 10 6), which is the x-coordinate of the N points on line y 1.
The fourth line has M numbers d 1, d 2, … , d M(0 <= d i < d i+1 <= 10 6), which is the x-coordinate of the M points on line y 2.
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
贪心题吧,外带一点数学知识。
两条线段,两堆点。然后找尽量多的三角形,求最多的三角形的时候连接线段的最小长度,两个边不相交。从左往右,记l和r为两条平行线第一个没被选的点。计算选l或选r新增的线的长度,选最小的。画个图就一目了然了。
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include<algorithm> using namespace std; double c[100005], d[100005]; int main() { int T, n, m, x, y; double a, b, h, len; scanf("%d", &T); for (int t = 1; t <= T; t++) { scanf("%lf%lf", &a, &b); h = fabs(a - b) * fabs(a - b); scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%lf", &c[i]); for (int i = 0; i < m; i++) scanf("%lf", &d[i]); x = y = 0; len = 0; while (x < n && y < m) { len += sqrt(h + fabs(c[x] - d[y]) * fabs(c[x] - d[y])); if (x == n - 1) y++; else if (y == m - 1) x++; else { if (fabs(c[x] - d[y + 1]) < fabs(c[x + 1] - d[y])) y++; else x++; } } printf("Case #%d: %.2f\n", t, len); } return 0; }
相关文章推荐
- poj 1754 I Hate It -- 线段树(卡时间啊啊啊啊啊)
- win8.1下创建开机启动项
- SpringMVC 通过post接收form参数或者json参数
- CodeForces - 667B Coat of Anticubism (数学几何规律)
- ubuntu 搭建伪分布式hadoop
- iOS开发报错CUICatalog: Invalid asset name supplied: (null)
- hdu1280前m大的数
- 数据库---那些不允许更新的视图
- jmeter笔记5
- 欢迎使用CSDN-markdown编辑器
- Spark生态之Alluxio学习1---单机版搭建和运行
- java死锁案例
- vtk实战(五十一)—构建三维形体
- OpenCV3.0 CMake生成工程项目过程中遇到下载ippicv_windows_20151201.zip错误
- 链式A+B之程序员面试经典
- PAT乙级—1021. 个位数统计 (15)-native
- WS_EX_TOOLWINDOW 属性的陷阱
- Chrome Cast研究记录
- springmvc源码解析(1)
- 338. Counting Bits--位运算