hdu4723 How Long Do You Have to Draw 贪心
来源:互联网 发布:什么网络歌手唱歌好听 编辑:程序博客网 时间:2024/05/29 09:17
How Long Do You Have to Draw
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 322 Accepted Submission(s): 128
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?
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.
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
10 12 31 30 2 4
Sample Output
Case #1: 5.66
既然先要满足三角形最多,那么肯定是要把所有的点能连的都连起来,然后重点是最短线段长度,如图
黑线是已经连接的线,x和y是当前位置,下一条线有两种连接方法,即绿线和蓝线,c[x]->d[y + 1]和 d[y]->c[x + 1],因为c[x]->d[y + 1]较短,所以选这条,y到y + 1的位置上去,然后把这条线段累加上,如果蓝线较短的话则将x放到x + 1的位置上去,然后把这条线段累加上,注意初始化和最后的边界,每一步都这样择优,最后输出的线段长度和一定最小
#include <cstdio>#include <cstring>#include <iostream>#include <cmath>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++;elsex++;}}printf("Case #%d: %.2f\n", t, len);}return 0;}
0 0
- hdu4723 How Long Do You Have to Draw 贪心
- HDU4723-How Long Do You Have to Draw
- hdu4723--(How Long Do You Have to Draw)
- hdu-4724-How Long Do You Have to Draw-贪心
- hdu 4723 How Long Do You Have to Draw(贪心)
- HDU 4723 How Long Do You Have to Draw
- hdoj-4723-How Long Do You Have to Draw
- HDU 4723 How Long Do You Have to Draw
- HDU 4723 How Long Do You Have to Draw 解题报告
- How to resolve: You do not have a license to use this ActiveX control
- Do I Have To Cry For You
- “you do not have permissions to run”
- How many fix bug method do you have?
- HOW TO DO WHAT YOU LOVE
- How bad do you want to succeed
- how do you do
- How To Find What You Love To Do
- 【转】How To Find What You Love To Do
- 在Fragment中嵌套Fragment时,布局不显示及the specified child already has parent错误
- 数据库知识
- Android关于PagerAdapter的使用方法的总结
- SQLite 设计与概念
- nodejs学习笔记2
- hdu4723 How Long Do You Have to Draw 贪心
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
- LeetCode 137 -Single Number II ( JAVA )
- JSTL中forEach标签中varStatus属性名列表
- MVC4.0 使用WebUploader上传文件实例,支持大视频上传
- 冒泡排序原理分析及Java实现
- win8安装Mac10.10
- ASCII表的使用
- expdp/impdp数据泵和RMAN全库备份shell脚本各一则