HDU - 4723 How Long Do You Have to Draw

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

题意: 给你两条平行x轴的线,第一条线给出n个点x坐标,第二条m个x点坐标,两条线的点全部连接,线段不能相交,满足形成三角形最多的同时,使线段和想加最小。

题解:贪心,因为形成三角形的数量一定,为n+m-2,因此我们先连接最左边的两个点,然后判断两条线上 下一个点和另一条线的 上一个点的距离哪个更小,连接小的,因为这样另一条线的那个点继续连接的话,距离也会减小

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int N=1e6+10;
double d;
int n,m;
double a[N],b[N];
int main()
{
    int T;
    int nn=1;
    scanf("%d",&T);
    while(T--)
    {
        double yy1,yy2;
        scanf("%lf%lf",&yy1,&yy2);d=fabs(yy1-yy2);
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
        for(int i=1;i<=m;i++)scanf("%lf",&b[i]);
        double ans=sqrt((a[1]-b[1])*(a[1]-b[1])+d*d);
        int i=2,j=2;
        while(i<=n&&j<=m)
        {
            if(sqrt(d*d+(a[i]-b[j-1])*(a[i]-b[j-1]))>sqrt(d*d+(b[j]-a[i-1])*(b[j]-a[i-1])))
                ans+=sqrt(d*d+(b[j]-a[i-1])*(b[j]-a[i-1])),j++;
            else
                ans+=sqrt(d*d+(a[i]-b[j-1])*(a[i]-b[j-1])),i++;
        }
        if(i<=n)for(;i<=n;i++) ans+=sqrt(d*d+(a[i]-b[m])*(a[i]-b[m]));
        if(j<=m)for(;j<=m;j++) ans+=sqrt(d*d+(a[n]-b[j])*(a[n]-b[j]));
        printf("Case #%d: %.2f\n",nn++,ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值