Cod sursa(job #81809)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 4 septembrie 2007 17:39:43
Problema Popandai Scor 4
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.46 kb
#include <stdio.h>
#include <algorithm>
#include <string>

using namespace std;

#define maxn 310
#define maxx 30010
#define maxv (1<<30)
#define LSB(x) ((x^(x-1))&x)

int n,m,l,sol;
int X,Y;
int a[maxn],b[maxn];
int s[maxn],p[maxn],q[maxn];
int c[maxx];
int under[maxn][maxn];
int v[maxn],u[maxn];

int cmp(int x,int y)
{
    return (a[x]-X)*(b[y]-Y)<(a[y]-X)*(b[x]-Y);
}

int cmp2(int x,int y)
{
    return (a[x]<a[y]) || ((a[x]==a[y]) && (b[x]<b[y]));
}

void update(int x)
{
     for (;x<maxx;x+=LSB(x)) c[x]++;
}

int query(int x)
{
    int rez=0;
    for (;x>0;x-=LSB(x)) rez+=c[x];
    
    return rez;
}

int nr(int p1,int p2,int p3)
{
    int rez=under[p1][p3]-under[p1][p2]-under[p2][p3];
    if (rez<0) return -rez;
    return rez-1;
}

int arie(int x1,int x2,int x3,int y1,int y2,int y3)
{
    return x1*y2+x2*y3+x3*y1-(x3*y2+x2*y1+x1*y3);
}

void prep(int pos)
{
     int i;
     X=a[pos],Y=b[pos];
     memset(c,0,sizeof(c));
     
     for (i=1;i<=l;i++) p[i]=i;
     
     sort(p+1,p+l+1,cmp);
     
     for (i=1;i<=l;i++)
     {
         under[pos][s[p[i]]]=query(b[s[p[i]]]+1);
         under[s[p[i]]][pos]=under[pos][s[p[i]]];
         update(b[s[p[i]]]+1);
     }
}

int main()
{
    freopen("popandai.in","r",stdin);
    freopen("popandai.out","w",stdout);
    
    scanf("%d %d ",&n,&m);
    
    int i,j,k,x,aux;
    
    for (i=1;i<=n;i++) 
    {
        scanf("%d %d ",&a[i],&b[i]);
        q[i]=i;
    }
    
    sort(q+1,q+n+1,cmp2);
    
    for (i=1;i<=n;i++)
    {
        l=0;
        for (j=i+1;j<=n;j++) s[++l]=q[j];
          
        prep(q[i]);
    }
    
    sol=maxv;
    
    for (i=1;i<=n;i++)
      for (j=1;j<=n;j++) 
        if (i!=j) 
        {  
            memset(v,-1,sizeof(v));
            memset(u,-1,sizeof(u));
          
            for (k=1;k<=n;k++)
              if ((k!=i) && (k!=j))
              {
                  x=nr(q[i],q[j],q[k]);
                  aux=arie(a[q[i]],a[q[j]],a[q[k]],b[q[i]],b[q[j]],b[q[k]]);
                  if (aux>=0) 
                  {
                     if ((v[x]==-1) || (aux<v[x])) v[x]=aux;
                  }
                  else if ((u[x]==-1) || (-aux<u[x])) u[x]=-aux;
              }
            
            for (k=0;k<=m;k++) 
              if ((v[k]!=-1) && (u[m-k]!=-1) && (v[k]+u[m-k]<sol)) sol=v[k]+u[m-k];
        }
        
    printf("%.1lf\n",1.0*sol/2);
    
    return 0;
}