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

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值