hdu 1199想用线段树,结果发现不好处理,某天突然想起这方法。。易懂,不好写。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <list>
#include <algorithm>

using namespace std;

struct node//区间结点
{
	int a,b;
	node():a(0),b(0){}
	node(int aa,int bb):a(aa),b(bb){}
	bool operator < (const node &bb)
	{  
		if(a==bb.a&&b<bb.b) return b<bb.b;
		return a<bb.a;
	}
};

list<node> G;//区间链表,本是想删除方便,后面发现自己不会用,纠结在于删除某迭代位置的元素后,迭代器将不可使用
inline int maxx(int a,int b)
{
	return a>b?a:b;
}
bool isnullset(node a)//判断是否是空区间
{
	if(a.a==0&&a.b==0) return true;
	return false;
}
node unions(node a,node b)//区间的交
{
	node temp;
	node temp1;
	if(a.a<b.a) {temp = a;temp1 = b;}
	else {temp = b; temp1 = a;}

	node en;
	if(temp1.a>temp.b) return en;
	if(temp1.b<=temp.b) return temp1;
	en.a=temp1.a;en.b=temp.b;
	return en;
}
void init()
{
	int n;
	while(cin>>n)
	{
		G.clear();

		while(n--)
		{
			int x,y;
			char ch;
			cin>>x>>y;getchar();cin>>ch;
			if(ch=='w')  {//白色直接插入,不进行各种合并
				node en(x,y);
				G.insert(G.end(),en);
			}
			else
			{
				list<node>::iterator it=G.begin();
				node temps(x,y);
				for(;it!=G.end();it++)
				{
					node ha= *it;
					node temp =unions(temps,ha);//对已有的白色区间与读入的黑色区间求交
					if(!isnullset(temp))//区间没交可直接忽略
					{
						if(temp.a==ha.a&&temp.b==ha.b) 
						{(*it).a = 0;(*it).b =0;}//不删除就赋值为空区间
						else if(temp.a==temp.b&&temp.a==ha.a)
							(*it).a = temp.a+1;
						else if(temp.a==temp.b&&temp.a==ha.b)
							(*it).b = temp.b-1;
						else if(temp.a==ha.a)
							(*it).a=temp.b+1;
						else if(temp.b==ha.b)
							(*it).b=temp.a-1;
						else//分成两个区间
						{
							int ri = (*it).b;
							(*it).b=temp.a-1;
							node en(temp.b+1,ri);
							G.insert(G.end(),en);
						}
					}

				}
			}
		}
		
		G.sort();//按x坐标排序
		
		list<node>::iterator  it;
	  /* for(it=G.begin();it!=G.end();it++)
		{
			cout<<(*it).a<<" "<<(*it).b<<endl;
		}*/

		node result;node cur;
		for(it=G.begin();it!=G.end();it++)
		{   
			if(isnullset(*it)) continue;//忽略空区间
			if(isnullset(cur)) //当前区间还从来没有赋过值
			{
				cur = *it;result = *it;
			}
			else
			{
				node temp = unions(cur,*it);//求与当前区间的交
				if(isnullset(temp))//区间交为空,表示当前区间可以放弃,并对结果进行更新
				{   
					if(cur.b+1 ==(*it).a) cur.b = (*it).b; 
					else if(cur.b-cur.a>result.b-result.a) {
						result = cur;
						cur = *it;
					}
					else cur = *it;
				}
				else//不为空,更新当前区间
				{
					cur.b = maxx((*it).b,cur.b);//这上面的还改了好几次!!!!思维不够密
				}
			}
			//if(it==G.end()--) 
				//if(cur.b-cur.a>result.b-result.a) result = cur;
		}
		if(cur.b-cur.a>result.b-result.a) result = cur;//对最后个当前区间进行辨别
		if(isnullset(result)) puts("Oh, my god");//结果为空说明没有最长的
		else
		cout<<result.a<<" "<<result.b<<endl;
	}
	return ;
}
int main()
{   
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	init();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值