HDU - 1199 Color the Ball (离散化入门题)

 

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

关于一些细节操作请参考下面博客网址,我只是单纯不想写线段树而已。。。

 参考博客:https://blog.csdn.net/QiuSun_ZhangYang/article/details/78159914

离散化我个人认为就是通过两个映射存起来真实值和离散化之后的值的一一对应的关系,这样操作的时候可以通过离散化之后的值对真实值所代表的东西进行操作,还是比较神奇的,先敲个板子慢慢理解。嘻嘻嘻嘻~~

#include <bits/stdc++.h>
using namespace std;
int a[2005],b[2005];
char c[2005];
int ball[13000];
vector<int>v;
map<int,int>mp1;//真实值到离散化后的值的映射关系
map<int,int>mp2;//离散化后的值到真实值的映射关系
int main()
{
	int n;
	while(cin>>n)
	{
        memset(ball,0,sizeof(ball));
        v.clear();
        mp1.clear();
        mp2.clear();
	for(int i=0;i<n;i++)
	{
		scanf("%d %d %c",&a[i],&b[i],&c[i]);
		v.push_back(a[i]);
		v.push_back(a[i]+1);
		v.push_back(a[i]-1);
		v.push_back(b[i]);
		v.push_back(b[i]+1);
		v.push_back(b[i]-1);
	}
	sort(v.begin(),v.end());
	int len=unique(v.begin(),v.end())-v.begin();
	for(int i=0;i<len;i++)
	{
            mp1[v[i]]=i+1;
            mp2[i+1]=v[i];
	}
        for(int i=0;i<n;i++)
        {
            int l=mp1[a[i]];
            int r=mp1[b[i]];
            if(c[i]=='w')
            {
                for(int j=l;j<=r;j++)
                {
                    ball[j]=1;
                }
            }else
            {
                for(int j=l;j<=r;j++)
                {
                    ball[j]=-1;
                }
            }
        }
        int ans=-1;
        int MAX_L,MAX_R;
        for(int i=1;i<=len;i++)
        {
            int j=i;
            int tmp;
            if(ball[i]==0||ball[i]==-1) continue;
            while(ball[j]==1) j++;
            tmp=mp2[j-1]-mp2[i]+1;
            if(tmp>ans)
            {
                ans=tmp;
                MAX_L=mp2[i];
                MAX_R=mp2[j-1];
            }
            i=j-1;
        }
        if(ans==-1)
        {
            cout<<"Oh, my god"<<endl;
        }else
        {
            cout<<MAX_L<<" "<<MAX_R<<endl;
        }
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值