HDU 1199.Color the Ball【区间操作(可以用离散化线段树)】【暴力求解】【5月26】

本文介绍了一道关于寻找最长连续白色球序列的算法题目,通过记录和更新白色球区间来高效解决该问题。文章提供了完整的C++实现代码,利用结构体存储区间,并通过排序和区间合并等操作找到最长白色序列。

Color the Ball

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


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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 2005;
struct ss
{
    int l, r;
    bool operator< (const ss a) const
    {
        return l < a.l;
    }
} L[MAX];
int N, l, r, k;
char wb;
int main()
{
    while(scanf("%d", &N) != EOF)
    {
        k = 0;
        while(N--)
        {
            scanf("%d %d %c", &l, &r, &wb);
            if(l > r) swap(l, r);
            if(wb == 'w')
            {
                L[k].l = l;
                L[k++].r = r;
            }
            else
            {
                int k2 = k;
                for(int i = 0; i < k2; ++i)
                {
                    if(l <= L[i].l && r >= L[i].l && r < L[i].r) L[i].l = r+1;
                    else if(l > L[i].l && l <= L[i].r && r >= L[i].r) L[i].r = l-1;
                    else if(l > L[i].l && r < L[i].r)
                    {
                        L[k].l = r+1;
                        L[k++].r = L[i].r;
                        L[i].r = l-1;
                    }
                    else if(l <= L[i].l && r >= L[i].r)
                    {
                        L[i].l = -1;
                        L[i].r = -1;
                    }
                }
            }
        }
        sort(L, L+k);
        int ans = 0, ansl, ansr;
        l = -1;
        r = -2;
        for(int i = 0; i < k; ++i)
        {
            if(!L[i].l == -1) continue;
            if(L[i].l > r+1)
            {
                if(r-l+1 > ans)
                {
                    ansl = l;
                    ansr = r;
                    ans = r-l+1;
                }
                l = L[i].l;
                r = L[i].r;
            }
            else r = max(r, L[i].r);
        }
        if(r-l+1 > ans)
        {
            ansl = l;
            ansr = r;
            ans = r-l+1;
        }
        if(ans == 0) cout <<"Oh, my god"<< endl;
        else cout << ansl <<" "<< ansr << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值