二分 点集中最近点对、周长最小三角形

这篇博客讨论了如何使用二分查找解决HDOJ上的两个几何问题:找到点集中最近的点对和构建周长最小的三角形。作者强调了在二分查找过程中判断条件的重要性,并分享了ACM比赛中的经验。代码实现中包含了排序和比较函数,以找到满足条件的点对和构建最小周长的三角形。
摘要由CSDN通过智能技术生成

HDOJ 1007  点集中 最近点对

:又是二分,在一个找中间点时没加判断条件,TIL了好多次,后来找到了这个小错误,AC。

学几何以来感觉到二分很强大。昨天晚上10级ACM选拨,想练练手感用二分查找去试着做N的N次方,但是数据太大,WA了。只好老实用对数方法做了。

HDOJ 3868  点集中 周长最小三角形

:杭电OJ不给力,学了这么久几何,唯一一次有可能在OJ比赛中A掉一个几何题,竟然突发状况,OJ爆掉了。赛后再A掉吧(赛后毫无压力地AC了)


图片

View Code

Problem : 1007 ( Quoit Design )     Judge Status : Accepted
RunId : 4210679    Language : G++    Author : ccsu2009021212
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta

#include <cmath > 
#include<iostream>
#include<stdio.h>
using namespace std;
const double INF  = 1E200;    
const double EP  = 1E-10; 
const double PI = acos(-1.0);
struct POINT 

 double x; 
 double y; 
 POINT(double a=0, double b=0) { x=a; y=b;} //constructor 
}point[100009];

double dist(POINT p1,POINT p2)                

 return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)  ); 

bool cmpx(POINT a,POINT b)
{
     return (a.x<b.x) ;
}
bool cmpy(int a,int b)
{
     return (point[a].y<point[b].y) ;
}
int ch[100009];
double shortest(int left,int right)//必须先要  sort(point,point+n,cmpx);
{
       if( right-left==1)
       return dist(point[left],point[right]);
       if( right-left==2)
       return min(min(dist(point[right],point[right-1]),dist(point[right-1],point[left])),dist(point[left],point[right]));
       int mid=(left+right)>>1;
       double temp=min(shortest(left,mid),shortest(mid+1,right));
       int len=0;
       for(int i=mid;i>=left&&temp>=point[mid+1].x-point[i].x;i--)
       ch[len++]=i;
       for(int i=mid+1;i<=right&&temp>=point[i].x-point[mid].x;i++)
       ch[len++]=i;
       sort(ch,ch+len,cmpy);
       for(int i=0;i<len;i++)
          for(int j=i+1;j<len&&(point[ch[j]].y-point[ch[i]].y)<=temp;j++)//这个退出条件很造成可能超时 
             temp=min(temp,dist(point[ch[j]],point[ch[i]]));
       return temp;
}
int main()
{
    int n;
    while( scanf("%d",&n)!=EOF&&n!=0)
    {
           for(int i=0;i<n;i++) 
                   scanf("%lf %lf",&point[i].x,&point[i].y); 
           sort(point,point+n,cmpx);
           double ans=shortest(0,n-1)/2.0;
           printf("%.2lf\n",ans);
           
    }
}

 

View Code

Problem : 3868 ( The Triangle ransmitter )     Judge Status : Accepted
RunId : 4236747    Language : G++    Author : ccsu2009021212
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include <cmath > #include<iostream> #include<stdio.h> using namespace std; const double INF = 1E200; const double EP = 1E-10; struct POINT { double x; double y; POINT(double a=0, double b=0) { x=a; y=b;} //constructor }point[200009]; double dist(POINT p1,POINT p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ); } double lenth(int p1,int p2,int p3) { return dist(point[p1],point[p2])+dist(point[p2],point[p3])+dist(point[p1],point[p3]); } bool cmpx(POINT a,POINT b) { return (a.x<b.x) ; } bool cmpy(int a,int b) { return (point[a].y<point[b].y) ; } double min(double a,double b) { return (a<b)?a:b; } int ch[2000009]; double ini; double shortest(int left,int right)//必须先要 sort(point,point+n,cmpx); { if( right-left<=1) return ini; if( right-left==2) return lenth(right,left,left+1); if(right-left==3) { return m
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用以下代码来计算三角形周长: #include <stdio.h> #include <math.h> int main() { float x1, y1, x2, y2, x3, y3; float a, b, c, perimeter; printf("请输入三个的二维坐标(x1, y1, x2, y2, x3, y3):\n"); scanf("%f%f%f%f%f%f", &x1, &y1, &x2, &y2, &x3, &y3); a = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); b = sqrt(pow(x3 - x2, 2) + pow(y3 - y2, 2)); c = sqrt(pow(x1 - x3, 2) + pow(y1 - y3, 2)); perimeter = a + b + c; printf("三角形周长为:%.2f\n", perimeter); return ; } ### 回答2: 要计算三角形周长,需要知道三个的坐标。假设这三个的坐标分别为A(x1, y1), B(x2, y2)和C(x3, y3)。 根据两间的距离公式,我们可以计算出三角形三条边的长度。假设边AB的长度为a,边BC的长度为b,边CA的长度为c。 使用勾股定理可以计算边长,即a = √((x2-x1)² + (y2-y1)²),b = √((x3-x2)² + (y3-y2)²),c = √((x1-x3)² + (y1-y3)²)。 最后,三角形周长就是a + b + c。 下面是一个示例程序,可以计算输入的三个构成三角形周长: ```python import math # 输入三个的坐标 x1, y1 = map(float, input('请输入第一个的坐标(以空格分隔):').split()) x2, y2 = map(float, input('请输入第二个的坐标(以空格分隔):').split()) x3, y3 = map(float, input('请输入第三个的坐标(以空格分隔):').split()) # 计算边长 a = math.sqrt((x2-x1)**2 + (y2-y1)**2) b = math.sqrt((x3-x2)**2 + (y3-y2)**2) c = math.sqrt((x1-x3)**2 + (y1-y3)**2) # 计算周长 perimeter = a + b + c print('三角形周长为:', perimeter) ``` 希望以上解答对您有帮助。 ### 回答3: 要求从键盘输入三个的二维坐标,来构成一个三角形,并计算出三角形周长。 首先,我们需要依次从键盘输入三个的坐标,并将它们保存起来。假设A的坐标为 (x1, y1),B的坐标为 (x2, y2),C的坐标为 (x3, y3)。 计算三条边的长度: - A到B的距离为 AB = √((x2 - x1)² + (y2 - y1)²); - B到C的距离为 BC = √((x3 - x2)² + (y3 - y2)²); - C到A的距离为 CA = √((x1 - x3)² + (y1 - y3)²)。 计算三角形周长: 将三条边的长度相加即可得到三角形周长:perimeter = AB + BC + CA。 最后,我们将计算的结果输出,即得到了三角形周长。 下面是一个示例代码: ``` import math # 从键盘输入三个的坐标 x1, y1 = map(float, input("请输入第一个的坐标:").split()) x2, y2 = map(float, input("请输入第二个的坐标:").split()) x3, y3 = map(float, input("请输入第三个的坐标:").split()) # 计算三条边的长度 AB = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) BC = math.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2) CA = math.sqrt((x1 - x3) ** 2 + (y1 - y3) ** 2) # 计算三角形周长 perimeter = AB + BC + CA # 输出结果 print("三角形周长为:", perimeter) ``` 通过以上代码,我们可以输入三个的坐标,计算得到三角形周长,并将其输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值