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

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
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值