hdu 1199 Color the Ball 离散线段树

本文详细解析了使用C++解决「最长白色球序列」问题的算法,包括输入输出规范、核心算法步骤及代码实现,旨在帮助初学者理解并掌握该问题的求解策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C - Color the Ball
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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 <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)

const int inf=0x7fffffff;   //无限大
char op[3];
struct interval
{
    int start,endn;
    bool operator < (const interval& b)const{
        if(start!=b.start)
            return start<b.start;
        else
            return endn<b.endn;
    }
};
interval val[20001];
void white(int a,int b,int& cnt)
{
    val[cnt].start=a;
    val[cnt].endn=b;
    cnt++;
}
void black(int a,int b,int& cnt)
{
    int tmp=cnt;
    for(int i=0;i<cnt;i++)
    {
        if(val[i].start<a)
        {
            if(val[i].endn>=a)
            {
                if(val[i].endn<=b)
                {
                    val[i].endn=a-1;
                }
                else
                {
                    val[tmp].start=b+1;
                    val[tmp].endn=val[i].endn;
                    tmp++;
                    val[i].endn=a-1;
                }
            }
        }
        else if(val[i].start<=b)
        {
            if(val[i].endn<=b)
            {
                val[i].start=0;
                val[i].endn=-1;
            }
            else
            {
                val[i].start=b+1;
            }
        }
    }
    cnt=tmp;
}
int solve(int cnt,int& index)
{
    sort(val,val+cnt);
    int maxn=val[0].endn-val[0].start+1;
    for(int i=1;i<cnt;i++)
    {
        if(val[i].start!=0)
        {
            if(val[i].start<=val[i-1].endn+1)
            {
                if(val[i-1].endn<=val[i].endn)
                {
                    val[i].start=val[i-1].start;
                }
                else
                {
                    val[i].start=val[i-1].start;
                    val[i].endn=val[i-1].endn;
                }
            }
            if(val[i].endn-val[i].start+1>maxn)
            {
                maxn=val[i].endn-val[i].start+1;
                index=i;
            }
        }
    }
    return maxn;
}
int main()
{
    int n,index,a,b,c;
    while(cin>>n)
    {
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%s",&a,&b,op);
            if(a>b)
            {
                swap(a,b);
            }
            if(op[0]=='w')
                white(a,b,cnt);
            else
                black(a,b,cnt);
        }
        index=0;
        if(solve(cnt,index))
        {
            cout<<val[index].start<<" "<<val[index].endn<<endl;
        }
        else
            cout<<"Oh, my god"<<endl;
    }
    return 0;
}

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值