dimanche 19 juin 2016

Selecting a triangle from given points such that every point lies strictly outside it

http://codeforces.com/problemset/problem/618/C

This is the exact problem. Given a number of points, we determine a triangle such that all other points lie strictly outside the triangle.

Constraints - 3<=n<10^5 10^-9

My approach -

  1. Select arbitrary point (say, the first point itself, let this be A)
  2. Find the nearest point to the selected point(B).
  3. Find the nearest point(C) to line AB

A,B,C are the required set of points. To find the point nearest to line, I minimize the area of the triangle being formed, where area is calculated by the area formula for cartesian points

( x[A] * (y[B] - y[C]) + x[B] * ( y[C] - y[A] ) + x[C] * ( y[A] - y[B] )

NOTE - 1/2 has not been included since if 2*L>2*K implies L>K Also, it brings the answer to floating values which would be undesirable.

Apparently, this is wrong. Where could I be going wrong? Following is my implementation

http://ideone.com/OLDsXV

#include<bits/stdc++.h>
#define Int long long
using namespace std;

int main(){

    Int n,i;
    cin>>n;
    Int x[n],y[n];

    for(i=0;i<n;i++)
        cin>>x[i]>>y[i];
    Int Min= LLONG_MAX,index;

    for(i=1;i<n;i++){

        Int temp = (x[i]-x[0])*(x[i]-x[0])+(y[i]-y[0])+(y[i]-y[0]);
        if(temp<Min){
            Min=temp;
            index=i;
        }

    }

    //cout<<"index "<<index<<endl;
    Int index2;
    Int Min2=LLONG_MAX;

    for(i=1;i<n;i++){


        if(i==index)
            continue;
        Int area;
        area = x[0]*(y[index]-y[i])+x[index]*(y[i]-y[0])+x[i]*(y[0]-y[index]);
        //area=abs(area);
        if(area<0)
            area*=-1;
        //cout<<"area "<<area<<endl;
        if(area<Min2&&area!=0){
            Min2=area;
            index2=i;
        }
          //  }

        //}
        //else continue;

    }

    //cout<<"index2 "<<index2<<endl;
    cout<<1<<' '<<index+1<<' '<<index2+1<<endl;


}

Aucun commentaire:

Enregistrer un commentaire