ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1019. Перекрашивание прямой

memcpy && memmove
Послано splutter 29 янв 2002 13:21
I got a WA when I have used memcpy, and I got accepted when I used
memmove....

why?
Re: memcpy && memmove
Послано Stefan Ciobaca 23 фев 2002 15:00
The only difference between memmove and memcpy is that
memmove handles overlapping memory, but memcpy doesn't.
if you got accepted pls help me
Послано Stefan Ciobaca 23 фев 2002 16:33
here is my program. I can't find any test data on which my program
fails so pls tell what is wrong:

#include <stdio.h>
#include <stdlib.h>

#define LEFT 0
#define RIGHT 1

#define MAX 5010

int n, heap[MAX], unde[MAX], nh, np, c[MAX * 2];

typedef struct {
    int x, y, color, k;
} segment;

typedef struct {
    int x, dir, seg;
} point;

segment d[MAX], best;

point p[MAX * 2];

void readinput()
{
    int i, a, b;
    char c;

#ifndef ONLINE_JUDGE
    freopen("linepaint.in", "r", stdin);
    freopen("linepaint.out", "w", stdout);
#endif
    scanf("%d", &n);
    d[0].x = 0;
    d[0].y = 1000000000;
    d[0].color = 'w' == 'w';
    d[0].k = 0;
    for (i = 1; i <= n; i++)
    {
        scanf("%d %d %c", &a, &b, &c);
        if (a != b)
        {
            d[i].x = a;
            d[i].y = b;
            d[i].color = c == 'w' || c == 'W';
            d[i].k = i;
        }
        else
        {
            i--;
            n--;
        }
    }
    n++;
//    printf("%d\n", n);
//    for (i = 0; i < n; i++)
//        printf("%d %d: %d\n", d[i].x, d[i].y, d[i].color);
//    printf("\n");
}

void adauga(int x, int dir, int seg)
{
    p[np].x = x;
    p[np].dir = dir;
    p[np].seg = seg;
    np++;
}

int compare(const void *a, const void *b)
{
    point *pa = (point *)a, *pb = (point *)b;

    if (pa->x < pb->x)
        return -1;
    else
        return 1;
}

#define GOUP(k) ((k) >> 1)
#define GOLEFT(k) ((k) << 1)
#define GORIGHT(k) (((k) << 1) + 1)

void fallback(int k)
{
    int maxim, l, r, temp;

    l = GOLEFT(k);
    r = GORIGHT(k);
    if (l <= nh && d[heap[l]].k > d[heap[k]].k)
        maxim = l;
    else
        maxim = k;
    if (r <= nh && d[heap[r]].k > d[heap[maxim]].k)
        maxim = r;
    if (maxim != k)
    {
        temp = heap[k];
        heap[k] = heap[maxim];
        heap[maxim] = temp;
        unde[heap[k]] = k;
        unde[heap[maxim]] = maxim;
        fallback(maxim);
    }
}

void add(int k)
{
    if (nh == 0)
    {
        heap[1] = k;
        unde[heap[1]] = 1;
        nh = 1;
    }
    else
    {
        heap[++nh] = heap[1];
        unde[heap[nh]] = nh;
        heap[1] = k;
        unde[heap[1]] = 1;
        fallback(1);
    }
}

void erase(int k)
{
    int temp;

    k = unde[k];
    temp = heap[k];
    heap[k] = heap[nh];
    heap[nh] = temp;
    unde[heap[k]] = k;
    nh--;
    fallback(k);
}

void solve()
{
    int i, temp;
    segment current;

    np = 0;
    for (i = 0; i < n; i++)
    {
        adauga(d[i].x, LEFT, i);
        adauga(d[i].y, RIGHT, i);
    }
    qsort(&p[0], np, sizeof(p[0]), &compare);
    nh = 0;
    best.x = 0;
    best.y = 1;
    best.color = 1;
    current.x = 0;
    current.y = 1;
    current.color = 1;
    for (i = 0; i < np; i++)
    {
        temp = p[i].x;
        while (p[i].x == temp && i < np)
        {
            if (p[i].dir == LEFT)
                add(p[i].seg);
            else
                erase(p[i].seg);
            i++;
        }
        if (i > 0 && i < np)
        {
            if (d[heap[1]].color != current.color)
            {
                current.x = p[i - 1].x;
                current.y = p[i].x;
                current.color = d[heap[1]].color;
                if (current.color == 1 && current.y -
current.x > best.y - best.x)
                    best = current;
            }
            else
            {
                current.y = p[i].x;
                if (current.color == 1 && current.y -
current.x > best.y - best.x)
                    best = current;
            }
//            printf("\n%d %d: %d", p[i - 1].x, p[i].x, d
[heap[1]].color);
        }
        i--;
    }
//    printf("\n\n");
}

void writeoutput()
{
    if (best.x >= best.y)
        for(;;);
    printf("%d %d\n", best.x, best.y);

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
}

int main()
{
    readinput();
    solve();
    writeoutput();
    return 0;
}
Re: if you got accepted pls help me
Послано Stefan Ciobaca 23 фев 2002 16:34
Never mind. I think I know where the trouble is. When I build the
heap I don't build it properly.