ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1245. Pictures

what's wrong with this code?(+)
Posted by James 16 Mar 2003 20:00
#include <stdio.h>
#include <string.h>

struct Tr{
    int r,x,y,lt,rt,up,dn;
};

Tr    p[1001];
long  s[1001],b[1001],s1[1001],b1[1001];
long  n,min,l,r,u,d,t1,t2,t3,tt;


void sort1(const int ll,int rr)
{
     long i,j;
     Tr xx,tt;

     i=ll; j=rr; xx=p[(ll+rr)/2];
     do
     {
         while ((p[i].lt<xx.lt) || (p[i].lt==xx.lt && p
[i].rt<xx.rt)) i++;
         while ((xx.lt<p[j].lt) || (xx.lt==p[j].lt && xx.rt<p
[j].rt)) j--;
         if (i<=j){
              tt=p[i]; p[i]=p[j]; p[j]=tt;
              i++; j--;
         }
     }while (i<=j);

     if (ll<j) sort1(ll,j);
     if (i<rr) sort1(i,rr);

}

void sort2(const int ll,int rr)
{
     long i,j;
     Tr xx,tt;

     i=ll; j=rr; xx=p[(ll+rr)/2];
     do
     {
         while ((p[i].dn<xx.dn) || (p[i].dn==xx.dn && p
[i].up<xx.up)) i++;
         while ((xx.dn<p[j].dn) || (xx.dn==p[j].dn && xx.up<p
[j].up)) j--;
         if (i<=j){
              tt=p[i]; p[i]=p[j]; p[j]=tt;
              i++; j--;
         }
     }while (i<=j);

     if (ll<j) sort2(ll,j);
     if (i<rr) sort2(i,rr);

}

long calc(long t1,long t2)
{
    if (t1<100) t1=100;
    if (t2<100) t2=100;
    return t1*t2;
}

void main()
{
    int i;

    scanf("%d",&n);
    l = 20000; r = -10000;
    d = 20000; u = -10000;
    for (i=1;i<=n;i++){
        scanf("%d%d%d",&p[i].r,&p[i].x,&p[i].y);
        p[i].lt=p[i].x-p[i].r;
        p[i].rt=p[i].x+p[i].r;
        p[i].up=p[i].y+p[i].r;
        p[i].dn=p[i].y-p[i].r;
        if (p[i].lt<l) l=p[i].lt;
        if (p[i].rt>r) r=p[i].rt;
        if (p[i].dn<d) d=p[i].dn;
        if (p[i].up>u) u=p[i].up;
    }
    t1 = r-l;
    t2 = u-d;
    min = calc(t1,t2);

    sort1(1,n);
    s[1]=p[1].dn; b[1]=p[1].up;
    tt = 0;
    for (i=2;i<=n;i++){
        if (p[i].dn<s[i-1]) s[i]=p[i].dn; else s[i]=s[i-1];
        if (p[i].up>b[i-1]) b[i]=p[i].up; else b[i]=b[i-1];
        if (p[i].rt>tt) tt=p[i].rt;
    }
    s1[n]=p[n].dn; b1[n]=p[n].up;
    for (i=n-1;i>=1;i--){
        if (p[i].dn<s1[i+1]) s1[i]=p[i].dn; else s1[i]=s1[i+1];
        if (p[i].up>b1[i+1]) b1[i]=p[i].up; else b1[i]=b1
[i+1];
    }
    for (i=1;i<n;i++)
        if (p[i].rt<=p[i+1].lt){
            t1 = p[i].rt-p[1].lt;
            t2 = b[i]-s[i];
            t3 = calc (t1,t2);
            t1 = tt-p[i+1].lt;
            t2 = b1[i+1]-s1[i+1];
            t3 = t3 + calc (t1,t2);
            if (t3<min) min=t3;
        }

    sort2(1,n);
    s[1]=p[1].lt; b[1]=p[1].rt;
    tt=0;
    for (i=2;i<=n;i++){
        if (p[i].lt<s[i-1]) s[i]=p[i].lt; else s[i]=s[i-1];
        if (p[i].rt>b[i-1]) b[i]=p[i].rt; else b[i]=b[i-1];
        if (p[i].up>tt) tt=p[i].up;
    }
    s1[n]=p[n].lt; b1[n]=p[n].rt;
    for (i=n-1;i>=1;i--){
        if (p[i].lt<s1[i+1]) s1[i]=p[i].lt; else s1[i]=s1[i+1];
        if (p[i].rt>b1[i+1]) b1[i]=p[i].rt; else b1[i]=b1
[i+1];
    }
    for (i=1;i<n;i++)
        if (p[i].up<=p[i+1].dn){
            t1 = p[i].up-p[1].dn;
            t2 = b[i]-s[i];
            t3 = calc (t1,t2);
            t1 = tt-p[i+1].dn;
            t2 = b1[i+1]-s1[i+1];
            t3 = t3 + calc (t1,t2);
            if (t3<min) min=t3;
        }

    printf("%d\n",min);
}