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
 
Recommend
zhuyuanchen520
 

题意:两条平行线。各有n、m个点。要连一些线,两个端点各自是两条平行线上的点。而且不能交叉。

在取得最多三角形的情况下,求最小的总的线的长度。

题解:要保证有最多的三角形 得把全部的点都连上。这样先把两平行线的最左端两点先连上,再依次把两条平行线最左端没连的选距离小的连上。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#define N 100010 using namespace std; double a[N],b[N]; 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;
}

hdu 4723 How Long Do You Have to Draw(贪心)的更多相关文章

  1. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  2. HDU 5742 It's All In The Mind (贪心)

    It's All In The Mind 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5742 Description Professor Zhan ...

  3. HDU 1789 Doing Homework again(非常经典的贪心)

    Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. HDU 5627 Clarke and MST &意义下最大生成树 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5627 题意:Bestcoder的一道题,让你求&意义下的最大生成树. 解法: 贪心,我们从高位 ...

  5. HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. HDU 2480 Steal the Treasure (并查集+贪心)

    题意:给你n个点,m条边,包括有向边与无向边,每条边都有一个权值.在每个点上都有一个人,他可以走与这个点直接相连的所有边中任意一条边一次,并且得到这个权值,就不能走了,注意这条路也只能被一个人走.问最 ...

  7. HDU 5037 Frog(2014年北京网络赛 F 贪心)

    开始就觉得有思路,结果越敲越麻烦...  题意很简单,就是说一个青蛙从0点跳到m点,最多可以跳l的长度,原有石头n个(都仅表示一个点).但是可能跳不过去,所以你是上帝,可以随便在哪儿添加石头,你的策略 ...

  8. HDU 5371 Hotaru&#39;s problem(Manacher算法+贪心)

    manacher算法详见 http://blog.csdn.net/u014664226/article/details/47428293 题意:给一个序列,让求其最大子序列,这个子序列由三段组成, ...

  9. hdu 4544 湫湫系列故事——消灭兔子 优先队列+贪心

    将兔子的血量从小到大排序,箭的威力也从小到大排序, 对于每仅仅兔子将威力大于血量的箭增加队列,写个优先队列使得出来数位价钱最少.. #include<stdio.h> #include&l ...

随机推荐

  1. C#实现HTML转图片(网页快照)

    有时候我们需要将网页转成图片,那么可以使用WebBrowser来生成网页快照,废话不多说,代码如下 1.网页快照帮助类(如果是BS或控制台需要引用System.Windows.Forms类库): pu ...

  2. (九)expect批量公钥推送

    (1)expect实现ssh非交互登录 注意:注释不能出现这脚本里面 spawn表示开启一个会话 \r:表示回车,exp_continue :表示没有出现这样,继续往下执行 interact :停留在 ...

  3. sonarQube Github pull request扫描代码

    参考官方地址:https://docs.sonarqube.org/display/PLUG/GitHub+Plugin 运行环境:sonarQube6.2 + sonarScanner2.8 近来, ...

  4. python 执行顺序

    从上往下顺序执行,定义的方法和类要写在调用之前, 如果有 if __name__ == '__main__'   改方法所在的文件作为启动文件时会被调用,如果作为模块被调用时不会被执行.

  5. (2)java安装配置

    java 分为三大类 javasSE,javaEE,javaME. javaSE:一般用于开发桌面软件,是java EE的基础类库 javaEE:用于开发网站 javaME:手机软件程序 javaSE ...

  6. ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)

    链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332 本文链接:http://www.cnblogs.com/Ash-l ...

  7. small test on 5.30 night T1

    数学题使劲推就对了. 让我们设  g(x) = ∑ C(i,x) * b^i ,然后后面验算了一张纸QWQ,懒得再打一遍了,回家我就把这张演算纸补上QWQ,先上代码. #include<cstd ...

  8. [LOJ6433]最大前缀和

    深刻感受到自己的水平和机房里的其他人相差甚远,他们都是随手秒这个题的... $n$很小,考虑状压DP 当一个序列在某个位置取到最大前缀和后,意味着如果把后面的数抽出来单独成序列,那么它的每个前缀和都$ ...

  9. openresty的时间获取

    ngx.say('ngx.time()' .. ngx.time()) ngx.say('ngx.now()' .. ngx.now()) ngx.say('ngx.today()' .. ngx.t ...

  10. log4j在Web项目中的使用

    导入log4j的jar包 在web.xml上配置如下:   <!-- 配置log4j begin -->   <context-param>     <param-nam ...