现在的位置: 首页 > 综合 > 正文

hdu1199Color the Ball (用链表做的,但没有AC,原因超内存)

2018年02月22日 ⁄ 综合 ⁄ 共 2253字 ⁄ 字号 评论关闭
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
3 1 4 w 8 11 w 3 5 b

Sample Output
8 11
#include<stdio.h>
#include<malloc.h>
typedef struct nn
{
    int l,r;
    struct nn *next;
}node,*NODE;
void creat(NODE &head)
{
    head=(NODE)malloc(sizeof(node));
    head->next=NULL;
    head->l=-1; head->r=-2;
}
void white(int l,int r,NODE head)
{
    if(head->r+1>=l)
    {
        if(head->l>l) head->l=l;
        if(head->r<r&&(head->next==NULL||head->next->l-1>r))
                    head->r=r;
        else while(head->next!=NULL&&head->next->l-1<=r)
        {
            NODE P=head->next;
              head->r=P->r; head->next=P->next;
              free(P);
        }
    }
    if(head->r+1<l)
    {
        if(head->next==NULL||head->next->l-1>r)
        {
            NODE p=(NODE)malloc(sizeof(node));
            p->l=l;p->r=r; p->next=head->next; head->next=p;
        }
        else
        white(l,r,head->next);
    }
}
void black(int l,int r,NODE head,NODE pre)
{
    if(head->r>=l)
    {
        if(head->l<l)
        {
            head->r=l-1;
            if(head->r>r)
            {
                NODE p=(NODE)malloc(sizeof(node));
                p->l=r+1; p->r=head->r;
                p->next=head->next; head->next=p;
            }
            if(head->next->l<=r&&head->next->r>r)
                    head->next->l=r+1;
            while(head->next!=NULL&&head->next->r<=r)
            {
                NODE P=head->next;
               head->next=P->next;
                free(P);
                if(head->next!=NULL&&head->next->l<=r&&head->next->r>r)
                    head->next->l=r+1;
            }
        }
        else
        {
            if(head->r>r) head->l=r+1;
            while(head!=NULL&&head->r<=r)
            {
                NODE P=head;
               pre->next=P->next; head=P->next;
                free(P);
                if(head!=NULL&&head->l<=r&&head->r>r)
                    head->l=r+1;
            }
        }
    }
    else
    black(l,r,head->next,head);
}
int p,q,len;
void find_max_len(NODE head)
{
    if(head==NULL) return ;
    int k=head->r-head->l+1;
    if(len<k)
    {
        p=head->l; q=head->r; len=k;
    }
    find_max_len(head->next);
}
int main()
{
    int n,l,r;
    char s;
    NODE head;
    while(scanf("%d",&n)>0)
    {
        creat(head);
        for(;n>0;n--)
        {
            scanf("%d%d %c",&l,&r,&s);
            if(s=='w') white(l,r,head);
            if(s=='b') black(l,r,head,head);
        }
        len=0;
        find_max_len(head);
        if(len==0)
        printf("Oh, my god\n");
        else
        printf("%d %d\n",p,q);
    }
}
/*
5
1 2 w
3 4 w
6 8 w
10 18 w
8 18 b
*/

抱歉!评论已关闭.