Color the Ball HDU - 1199(离散化)

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<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#define INF 0x3f3f3f
#define N 3000
using namespace std;
struct node{
    int l,r;
}haha[10005];
int cmp(node x1,node x2)
{
    return x1.l<x2.l;
}
int n;
int len;
int main()
{
    int x,y;
    char c[10];
    while(scanf("%d",&n)==1)
    {
        len=0;
        while(n--)
        {
            scanf("%d%d%s",&x,&y,&c);
            if(x>y)
            {
                x^=y;
                y^=x;
                x^=y;
            }
            if(c[0]=='w')
                haha[len].l=x,haha[len++].r=y;//白色就保存
            else
            {
                int now=len;
                for(int i=0;i<now;i++)//对每个区间进行判断,是否有交集
                {
                    if(x<=haha[i].l&&haha[i].l<=y)
                    {
                        if(haha[i].r<=y)
                        {
                            haha[i].r=0;
                            haha[i].l=0;//如果当前的黑区间完全覆盖了这个白色区间,那么这个区间就是不存在了,所以赋值为0,作为标志量
                        }
                        else
                            haha[i].l=y+1;

                    }
                    if(haha[i].l<x)
                    {
                        if(x<=haha[i].r&&haha[i].r<=y)
                            haha[i].r=x-1;
                        if(haha[i].r>y)
                        {
                            haha[len].l=y+1;
                            haha[len].r=haha[i].r;
                            len++;
                            haha[i].r=x-1;
                        }
                    }
                }
            }
        }
        sort(haha,haha+len,cmp);
        int now=0;
        for(now=0;now<len;now++)
        {
            if(!haha[now].l)
                break;
        }
        if(now==len)//如果区间都是0,那么答案不存在
        {
            printf("Oh, my god\n");
            continue;
        }
        int L,R;
        L=haha[now].l,R=haha[now].r;
        int maxL=L,maxR=R;
        for(now++;now<len;now++)//这里是合并区间了
        {
            if(haha[now].l<=R+1)
            {
                R=max(R,haha[now].r);//如果区间可以连接或覆盖,更新R值
            }
            else
            {
                if(maxR-maxL<R-L)//如果当前区间没有交集,说明之前的区间已经是一个独立的区间了
                {
                    maxR=R;
                    maxL=L;
                }
                L=haha[now].l,R=haha[now].r;
            }
        }
        if(maxR-maxL<R-L)
        {
            maxR=R;
            maxL=L;
        }//最后这里还需要在判断一次
        printf("%d %d\n",maxL,maxR);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值