ZOJ2301(HDU1199) Color the Ball(线段树离散化)

来源:互联网 发布:红叶知弦h同人 编辑:程序博客网 时间:2024/06/06 08:45

http://acm.hdu.edu.cn/showproblem.php?pid=1199

Color the Ball

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4688    Accepted Submission(s): 1164


Problem Description
There are infinite balls in a line (numbered 1 2 3 ....), and initially all of them are paint black. Now Jim use a brush paint the balls, every time give two integers a b and follow by a char 'w' or 'b', 'w' denotes the ball from a to b are painted white, 'b' denotes that be painted black. You are ask to find the longest white ball sequence.
 

Input
First line is an integer N (<=2000), the times Jim paint, next N line contain a b c, c can be 'w' and 'b'.

There are multiple cases, process to the end of file.
 

Output
Two integers the left end of the longest white ball sequence and the right end of longest white ball sequence (If more than one output the small number one). All the input are less than 2^31-1. If no such sequence exists, output "Oh, my god".
 

Sample Input
31 4 w8 11 w3 5 b
 

Sample Output
8 11
 

Author
ZHOU, Kai
 

Source
ZOJ Monthly, February 2005
 
#include <iostream>#include <algorithm>using namespace std;struct Seg{    int start;    int end;};inline int cmp(const Seg a, const Seg b){    return a.start > b.start;}int main(void){    int n, len, tmp;    int a, b, tmpsize, maxsize, tstart, tend, ansstart, ansend;    char ch;    Seg line[20010];    while (cin >> n)    {        len = 0;        while (n--)        {            cin >> a >> b >> ch;            if (a > b) swap(a, b);            if (ch == 'w')            {                line[len].start = a;                line[len].end = b;                len++;            }            else            {                tmp = len;                for (int i = 0; i < tmp; ++i)                {                    if (a <= line[i].start && b >= line[i].start && b < line[i].end)                    {                        line[i].start = b + 1;                        continue;                    }                    if (a > line[i].start && a <= line[i].end && b >= line[i].end)                    {                        line[i].end = a - 1;                        continue;                    }                    if (a > line[i].start && b < line[i].end)                    {                        line[len].start = b + 1;                        line[len].end = line[i].end;                        line[i].end = a - 1;                        len++;                        continue;                    }                    if (a <= line[i].start && b >= line[i].end)                    {                        line[i].start = -1;                        line[i].end = -1;                    }                }            }        }        sort(line, line + len, cmp);        tstart = -1;        tend = -2;        maxsize = 0;        for (int i = 0; i < len; ++i)        {            if (line[i].start != -1)            {                if (line[i].start > tend + 1)                {                    tmpsize = tend - tstart + 1;                    if (tmpsize > maxsize)                    {                        maxsize = tmpsize;                        ansstart = tstart;                        ansend = tend;                    }                    tstart = line[i].start;                    tend = line[i].end;                }                else tend = tend > line[i].end ? tend : line[i].end;            }        }        tmpsize = tend - tstart + 1;        if (tmpsize > maxsize)        {            maxsize = tmpsize;            ansstart = tstart;            ansend = tend;        }        if (maxsize == 0) cout << "Oh,  my god" << endl;        else cout << ansstart << " " << ansend << endl;    }    return 0;}


0 0
原创粉丝点击