hdu 4723 How Long Do You Have to Draw(贪心)
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
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?
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.
1
0 1
2 3
1 3
0 2 4
Case #1: 5.66
题意:两条平行线。各有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(贪心)的更多相关文章
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- 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 ...
- HDU 1789 Doing Homework again(非常经典的贪心)
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 5627 Clarke and MST &意义下最大生成树 贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5627 题意:Bestcoder的一道题,让你求&意义下的最大生成树. 解法: 贪心,我们从高位 ...
- HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 2480 Steal the Treasure (并查集+贪心)
题意:给你n个点,m条边,包括有向边与无向边,每条边都有一个权值.在每个点上都有一个人,他可以走与这个点直接相连的所有边中任意一条边一次,并且得到这个权值,就不能走了,注意这条路也只能被一个人走.问最 ...
- HDU 5037 Frog(2014年北京网络赛 F 贪心)
开始就觉得有思路,结果越敲越麻烦... 题意很简单,就是说一个青蛙从0点跳到m点,最多可以跳l的长度,原有石头n个(都仅表示一个点).但是可能跳不过去,所以你是上帝,可以随便在哪儿添加石头,你的策略 ...
- HDU 5371 Hotaru's problem(Manacher算法+贪心)
manacher算法详见 http://blog.csdn.net/u014664226/article/details/47428293 题意:给一个序列,让求其最大子序列,这个子序列由三段组成, ...
- hdu 4544 湫湫系列故事——消灭兔子 优先队列+贪心
将兔子的血量从小到大排序,箭的威力也从小到大排序, 对于每仅仅兔子将威力大于血量的箭增加队列,写个优先队列使得出来数位价钱最少.. #include<stdio.h> #include&l ...
随机推荐
- 为当前用户创建cron服务
为当前用户创建cron服务 1. 键入 crontab -e 编辑crontab服务文件 例如 文件内容如下: */2 * * * * /bin/sh /home/admin/jiaoben/bu ...
- 集训考试题tents
题目描述Pb 去郊游啦!他来到一块空地打算在这里搭一个帐篷.但是,帐篷的四个支撑点不能在落在任何位置上,而只能落在一些固定点上.现在,他找到地面上有 N 个点可以支撑帐篷.(四个支撑点必须围成一个矩形 ...
- luoguP2296 寻找道路
因为是出边与终点直接或间接相连,所以将边反向,从终边开始,将所有终边能到达的点都打上标记因为是最短路,所以不需要处理重边和自环,于是再跑最短路就好题目关键:路径上的所有点的出边所指向的点都直接或间接与 ...
- ASP.NET Core 2.2 基础知识(三) 静态文件
什么是静态文件? HTML,CSS,JS,图片等都叫做静态文件. 要想提供静态文件给客户端,需要注册静态文件中间件. 我们先分别添加一个 WebAPI 项目,一个 Razor 视图项目,比较两个项目的 ...
- 对mysql数据库表的相关操作
虫师博客(Python使用MySQL数据库(新)): https://www.cnblogs.com/fnng/p/3565912.html 1.更改表的结构,增加一个字段放置新增的属性 alter ...
- Linux CentOS下Python+robot framework环境搭建
转载自:http://blog.sina.com.cn/s/blog_13cc013b50102vof1.html 操作系统环境:CentOS 6.5-x86_64 下载地址:http://www.c ...
- 将Java程序打jar包并运行
1)接着上篇博客继续说手动编译之后,将代码打成jar包,然后直接“java -jar lz.jar"运行不成功的问题.还是先上代码: 这个是Demo类: package org.lz.dem ...
- winform如何保持TreeView节点展开和折叠的状态
转载:http://blog.sina.com.cn/s/blog_6abcacf5010138q5.html private Hashtable NodesStatus = new Hashtabl ...
- ZooKeeper服务器是用Java创建的,它在JVM上运行。
ZooKeeper服务器是用Java创建的,它在JVM上运行. 创建配置文件 使用命令 vi conf/zoo.cfg 和所有以下参数设置为起点,打开名为 conf/zoo.cfg 的配置文件. $ ...
- WebHelper-SessionHelper、CookieHelper、CacheHelper、Tree
ylbtech-Unitity: cs-WebHelper-SessionHelper.CookieHelper.CacheHelper.Tree SessionHelper.cs CookieHel ...