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. 如何在qt中使用中文输入法

    参考: http://blog.csdn.net/u013812682/article/details/52101088 dpkg -L fcitx-frontend-qt5 到qt安装目录里find ...

  2. AC日记——[Noi2011]阿狸的打字机 bzoj 2434

    2434 思路: 构建ac自动机: 抽离fail树: 根据字符串建立主席树: 在线处理询问: 询问x在y中出现多少次,等同于y有多少字母的fail能走到x: 1a,hahahahah: 代码: #in ...

  3. php(间接)调用nmap命令时的选项特殊点

    使用php调用pythn-nmap时,发现无法正常执行扫描动作 将nmap命令直接写入php,由后者调用,发现仍然无法执行,提示需要添加“-Pn”选项 原来是: 正常情况下:nmap -n host ...

  4. 【转】持续集成 Sonar 平台搭建及 Sonar 自定义规则打包部署篇

    引言 基于阿里开发手册的sonar自定义插件工程 开源地址: https://github.com/tigerge000/sonar-java-custom-rules.git由于最近来问童鞋,就算写 ...

  5. 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)

    A. An Olympian Math Problem 54.28% 1000ms 65536K   Alice, a student of grade 66, is thinking about a ...

  6. Hive知识

    HIVEQL CREATE DATABASE financials(创建数据库) SHOW DATABASES(显示数据库) SHOW TABLES IN 数据库(列出数据库的所有表) SHOW DA ...

  7. 线段树维护矩阵【CF718C】 Sasha and Array

    Description 有一个长为\(n\)的数列\(a_{1},a_{2}...a_{n}\),你需要对这个数列维护如下两种操作: \(1\space l \space r\space x\) 表示 ...

  8. 谜题15:令人晕头转向的Hello

    下面的程序是对一个老生常谈的例子做出了稍许的变化之后的版本.那么,它会打印出什么呢? /** * Generated by the IBM IDL-to-Java compiler, version ...

  9. 【set】【multiset】bzoj1058 [ZJOI2007]报表统计

    对n个位置,每个位置维护一个vector. 每次插入,可能对MIN_SORT_GAP产生的影响,只可能是 插入元素 和 它的 前驱 后继 造成的,用一个set维护(存储所有序列中的元素). 我们还得维 ...

  10. [HDU6252]Subway Chasing

    题目大意: 一条直线上有n个点,两个人在直线上走,保持x的距离. 告诉你m条信息,告诉你一个人在ab之间时,另一个人在cd之间. 问这些信息是否矛盾,如果不矛盾,求相邻两点之间的最小距离. 思路: m ...