[技巧性枚举] ZOJ 3672 Pan's Labyrinth

滕星纬
2023-12-01

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3762

Pan's Labyrinth

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge

Ofelia is chased by her evil stepfather, and now she finds herself lost in a labyrinth. She needs your help to run away from her tragic family.

There's a huge metal door standing in front of the exit of the labyrinth. There are n dots on the metal door. Pan (the god of the labyrinth) asks Ofelia to find out a triangle which has the largest height. The triangle's three vertexes must be the dots on the door, and its area must be positive. Ofelia should tell Pan the triangle's height so Pan will let Ofelia go.

Input

There are multiple cases (About 100 cases). For each case, the first line contains an integer n (3<=n<=500). In the next n lines, each line contains two real number x[i]y[i] (0<=x[i], y[i]<=10000) which indicates each dot's coordinate. There is no two dots in the same coordinate. The answers are strictly greater than 0.

Output

For each test case, output one line with the maximum triangle height. Any solution with a relative or absolute error of at most 1e-6 will be accepted.

Sample Input

6
0.000 4.000
2.000 4.000
1.000 0.000
1.000 8.000
0.900 7.000
1.100 7.000
7
6967.54555 3457.71200
3.52325 1273.85912
7755.35733 9812.97643
753.00303 2124.70937
7896.71246 8877.78054
5832.77264 5213.70478
4629.38110 8159.01498

Sample Output

7.00000
8940.96643

Hint

In case 1. Choose the  dot 3, 5, 6  to make up a triangle, and this triangle has the largest height 7.000. In case 2. We choose dot 2, 3, 5.


题目意思:

给n个点,选3个点组成一个三角形,求出最长的高。

解题思路:

高最长的三角形的底边的两点中必有一个点是,距离顶点最远距离的点。证明见:http://blog.csdn.net/synapse7/article/details/20942443
先预处理出最远的点,far[i]表示距离点i最远的点,o(n)枚举其中一个点,o(1)找出最远距离的点,再o(n)枚举另一个点。

叉积求面积,再求高。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 550

struct Point
{
    double x,y;
}pp[Maxn];
double dis[Maxn][Maxn];
int far[Maxn],n; //far[i]表示距离点最远的点的坐标

double Cal(int i,int j) //计算两点的距离
{
    return sqrt((pp[i].x-pp[j].x)*(pp[i].x-pp[j].x)+(pp[i].y-pp[j].y)*(pp[i].y-pp[j].y));

}

double Cha(Point a,Point b) //计算两个向量的叉积
{
    return a.x*b.y-a.y*b.x;
}

double solve(int a,int b,int c)
{
    Point aa,bb;
    aa.x=pp[a].x-pp[b].x,aa.y=pp[a].y-pp[b].y;
    bb.x=pp[c].x-pp[b].x,bb.y=pp[c].y-pp[b].y;

    double are=abs(Cha(aa,bb)); //面积的2倍

    return max(are/dis[a][b],max(are/dis[a][c],are/dis[b][c])); //最长的高

}

int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   while(~scanf("%d",&n))
   {
       for(int i=1;i<=n;i++)
            scanf("%lf%lf",&pp[i].x,&pp[i].y);
       for(int i=1;i<=n;i++) //预处理出每个点最远距离的点
       {
           double dd=-1;
           int re;

           for(int j=1;j<=n;j++)
           {
               if(i==j)
                    continue;
               double temp=Cal(i,j);
               dis[i][j]=temp;

               if(temp>dd)
               {
                   dd=temp;
                   re=j;
               }
           }
           far[i]=re;
       }
       double ans=-1;

       for(int i=1;i<=n;i++)
       {
           int ff=far[i];

           for(int j=1;j<=n;j++)
           {
               if(j==i||j==ff)
                    continue;
               double temp=solve(i,j,ff);
               if(temp>ans)
                   ans=temp;
           }
       }
       printf("%.6lf\n",ans);
   }

   return 0;
}




 类似资料: