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 1188. Library

Can't get AC. Can anyone help me? Here's my src...
Posted by Alyosha Popovich 9 May 2002 15:42
#include <stdio.h>

struct shelf
{
    int x, y, l, x1, x2;
} S[501];

int XN, YN, XT, YT;
int N;
long Ra = 666666, Rb;

void move(shelf s, int am, long &a, long &b)
{
    int x;
    a = b = 0;
    s.x += am;
    s.x1 -= am;
    s.x2 -= am;

    if (s.x < 0)
    {
        x = -s.x;
        s.x += x;
        s.x1 -= x;
        s.x2 -= x;
        s.l -= x;
        b += x;
    }
    if (s.x + s.l > XN)
    {
        x = s.x + s.l - XN;
        s.l -= x;
        b += x;
    }
    if (s.x2 < 0 || s.x1 > s.l || s.l <= 0)
    {
        a += 2;
        b += s.l;
        return;
    }
    if (s.x1 < 0 || s.x2 > s.l)
    {
        a++;
        return;
    }
    if (s.l/2 < s.x1)
        b += 2 * (s.x1 - s.l/2);
    else
    if ((s.l+1)/2 > s.x2)
        b += 2 * ((s.l+1)/2 - s.x2);
}


void taie(shelf s, int x, int y, long &a, long &b)
{
    int px1, px2;
    long a1, b1, a2, b2;
    if (s.y <= y || s.y >= y+YT || s.x+s.l <= x || s.x >= x+XT)
return;
    move(s, x+XT-s.x, a1, b1);
    move(s, x-s.x-s.l, a2, b2);
    if (a2 < a1 || (a2 == a1 && b2 < b1))
       a1 = a2, b1 = b2;
    a += a1, b += b1;
}

void incearca(int x, int y)
{
    int i;
    long a, b;
    a = b = 0;
    for (i = 1; i <= N; i++)
        taie(S[i], x, y, a, b);
    if (a < Ra || (a == Ra && b < Rb))
       Ra = a, Rb = b;
}

void solve()
{
    int i, x;
    incearca(4, 1);
    for (i = 1; i <= N; i++)
    if (S[i].y+YT <= YN)
        for (x = 0; x <= S[i].l-XT; x++)
            incearca(S[i].x+x, S[i].y);
}


void read_data()
{
    int i;
    scanf("%d %d %d %d %d", &XN, &YN, &XT, &YT, &N);
    for (i = 1; i <= N; i++)
        scanf("%d %d %d %d %d", &S[i].y, &S[i].x, &S[i].l, &S[i].x1,
&S[i].x2);
}


void main()
{
    read_data();
    solve();
    printf("%ld %ld\n", Ra, Rb);
}