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 1019. Line Painting

[solved] WA #6, can't figure out what's wrong
Posted by qkthePmj 4 Jan 2011 05:12
Good time of day.
I constantly get WA on test #6. Does anybody know why may this happen?
Here's my code, seems to give correct answers for any correct test found in discussions:

#include <stdio.h>
#include <iostream>
#include <map>

using namespace std;

typedef unsigned int uint;

const uint MAX_N = 5000;
const bool COLOR_BLACK = false;
const bool COLOR_WHITE = true;

typedef pair <uint, bool> LinePair;
typedef map<uint, LinePair> TLines;

TLines lines;

void paint(uint c, uint d, const bool clr)
{
    if (c == d)
        return;
    uint a, b;
    bool c2;
    if (lines.empty())
    {
        lines[c] = LinePair(d, clr);
        return;
    }

    TLines::iterator i = lines.begin();
    //пропускаем все отрезки, что левее нашего и не накладываются на него
    while (c > (i->second).first) ++i;
    //работаем с накладывающимися отрезками
    while ((d >= i->first) && (i != lines.end()))
    {
        //для удобства написания
        a = i->first;
        b = (i->second).first;
        c2 = (i->second).second;
        if (a >= c)
        {
            if (d >= b)
            {
                lines.erase(i);
            }
            else if (clr == c2)
            {
                d = b;
                lines.erase(i);
            }
            else
            {
                lines.erase(i);
                lines[d] = LinePair(b, c2);
            }
        }
        else
        {
            if (d >= b)
            {
                if (clr == c2)
                {
                    c = a;
                    lines.erase(i);
                }
                else
                {
                    (i->second).first = c;
                }
            }
            else if (clr == c2)
            {
                c = a;
                d = b;
            }
            else
            {
                (i->second).first = c;
                lines[d] = LinePair(b, c2);
            }
        }
        ++i;
    }
    //собственно добавление заданного отрезка
    lines[c] = LinePair(d, clr);
    return;
}

int main()
{
    uint n, b, e;
    char ch;
    bool clr;
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
#endif
    lines.clear();
    paint(0, 1e+9, COLOR_WHITE);
    cin >> n;
    for (uint i = 0; i < n; i++)
    {
        cin >> b >> e >> ch;
        if (ch == 'b')
            clr = COLOR_BLACK;
        else
            clr = COLOR_WHITE;
        paint(b, e, clr);
    }
    TLines::const_iterator maxLine;
    uint max = 0;
    for (TLines::const_iterator i = lines.begin(); i != lines.end(); ++i)
    {
        if (((i->second).second == COLOR_WHITE) && ((i->second).first - i->first > max))
        {
            max = (i->second).first - i->first;
            maxLine = i;
        }
#ifndef ONLINE_JUDGE
        cout << i->first << '\t' << (i->second).first << '\t' << (i->second).second << endl;
#endif
    }
    cout << maxLine->first << " " << (maxLine->second).first << endl;
    return 0;
}

Edited by author 05.01.2011 03:29
Re: WA #6, can't figure out what's wrong
Posted by qkthePmj 5 Jan 2011 03:28
I've got AC. I just needed not to use iterator after erasing it. But the solution is far from optimal, I'm gonna rewrite it.
Re: WA #6, can't figure out what's wrong
Posted by term 13 Jan 2011 14:52
AC

#include <stdio.h>
#include <iostream>
#include <map>

using namespace std;

typedef unsigned int uint;

const uint MAX_N = 5000;
const bool COLOR_BLACK = false;
const bool COLOR_WHITE = true;

typedef pair <uint, bool> LinePair;
typedef map<uint, LinePair> TLines;

TLines lines;

void paint(uint c, uint d, const bool clr)
{
    if (c == d)
        return;
    uint a, b;
    bool c2;
    if (lines.empty())
    {
        lines[c] = LinePair(d, clr);
        return;
    }

    TLines::iterator i = lines.begin();
    //пропускаем все отрезки, что левее нашего и не накладываются на него
    while (c > (i->second).first) ++i;
    //работаем с накладывающимися отрезками
    while ((d >= i->first) && (i != lines.end()))
    {
        //для удобства написания
        a = i->first;
        b = (i->second).first;
        c2 = (i->second).second;
        if (a >= c)
        {
            if (d >= b)
            {
                i = lines.erase(i);
            }
            else if (clr == c2)
            {
                d = b;
                i = lines.erase(i);
            }
            else
            {
                i = lines.erase(i);
                lines[d] = LinePair(b, c2);
            }
        }
        else
        {
            if (d >= b)
            {
                if (clr == c2)
                {
                    c = a;
                    i = lines.erase(i);
                }
                else
                {
                    (i->second).first = c;
                    i++;

                }
            }
            else if (clr == c2)
            {
                c = a;
                d = b;
                i++;
            }
            else
            {
                (i->second).first = c;
                lines[d] = LinePair(b, c2);
                i++;
            }
        }
    }
    //собственно добавление заданного отрезка
    lines[c] = LinePair(d, clr);
    return;
}

int main()
{
    uint n, b, e;
    char ch;
    bool clr;
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
#endif
    lines.clear();
    paint(0, 1e+9, COLOR_WHITE);
    cin >> n;
    for (uint i = 0; i < n; i++)
    {
        cin >> b >> e >> ch;
        if (ch == 'b')
            clr = COLOR_BLACK;
        else
            clr = COLOR_WHITE;
        paint(b, e, clr);
    }
    TLines::const_iterator maxLine;
    uint max = 0;
    for (TLines::const_iterator i = lines.begin(); i != lines.end(); ++i)
    {
        if (((i->second).second == COLOR_WHITE) && ((i->second).first - i->first > max))
        {
            max = (i->second).first - i->first;
            maxLine = i;
        }
#ifndef ONLINE_JUDGE
        cout << i->first << '\t' << (i->second).first << '\t' << (i->second).second << endl;
#endif
    }
    cout << maxLine->first << " " << (maxLine->second).first << endl;
    return 0;
}