URAL 1019 Line Painting(解题报告)

Description

The segment of numerical axis from 0 to 10 9 is painted into white color. After that some parts of this segment are painted into black, then some into white again and so on. In total there have been made  N re-paintings (1 ≤  N ≤ 5000). You are to write a program that finds the longest white open interval after this sequence of re-paintings.

Input

The first line of input contains the only number  N. Next  N lines contain information about re-paintings. Each of these lines has a form:
ai bi ci
where  ai and  bi are integers,  ci is symbol ‘b’ or ‘w’,  aibici are separated by spaces.
This triple of parameters represents repainting of segment from  ai to  bi into color  ci (‘w’ — white, ‘b’ — black). You may assume that 0 < ai <  bi < 10 9.

Output

Output should contain two numbers  x and  y ( x <  y) divided by space(s). These numbers should define the longest white open interval. If there are more than one such an interval output should contain the one with the smallest  x.

Sample Input

input output
4
1 999999997 b
40 300 w
300 634 w
43 47 b
47 634
这个题目由于数据太大,所以要离散化,具体什么事离散化,参见百度,谷歌。不过看完了下面的程序估计也理解的差不多 了,离散化后来一次染色,然后找最大的区间就好了,具体见程序注解吧。
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define MAXN 5001
inline int L(int r) {return r<<1;}
inline int R(int r) {return (r<<1)+1;}
 
struct point 
{
	int l_or_r,value;
};
 
point segment[MAXN*2];
char s[MAXN*2];
int post[MAXN*2][2];
int value[MAXN*2];
int color[MAXN*2];
 
int cmp(point a,point b)
{
	return a.value<b.value;
}
 
void colorful(int a,int b,int colors)
{
	for(int i=a;i<b;i++)
	{
		color[i]=colors;
	}
}
 
int main()
{
	int t,i;
	segment[L(0)].l_or_r = -0-1;//左值就标记为负数 
	segment[R(0)].l_or_r = 0+1;//右值标记为正数 
	segment[L(0)].value = 0;//记录真实值 
	segment[R(0)].value = 1000000000;
	s[0]='w';
	scanf("%d",&t);
	for(i=1;i<=t;i++)
	{
		//cout<<i<<endl;
		int templ,tempr;
		scanf("%d %d %c",&templ,&tempr,&s[i]);
		segment[L(i)].l_or_r = -i-1;//左值就标记为负数
		segment[R(i)].l_or_r = i+1;//右值标记为正数
		segment[L(i)].value = templ;//记录真实值 
		segment[R(i)].value = tempr;
	}
	sort(segment,segment+t*2+2,cmp);//把真实值按照从小到大排序 
	/*for(i=0;i<t*2+2;i++)
	{
		printf("%d ",segment[i].value);
	}*/
 
	int count=-1,temp=-1;
	for(i=0;i<t*2+2;i++)//开始离散化,重复的点合并 
	{
		if(temp!=segment[i].value)
		{
			count++;
			temp=segment[i].value;
		}
		if(segment[i].l_or_r<0)
		{
			post[-segment[i].l_or_r-1][0] = count;
		}
		else 
		{
			post[segment[i].l_or_r-1][1] = count;
		}
		value[count] = segment[i].value;//记录离散后的真实值 
	}
 
	/*for(i=0;i<=count;i++)
	{
		cout<<value[i]<<' ';
	}
	cout<<endl;
	for(i=0;i<5;i++)
	{
		cout<<post[i][0]<<' '<<post[i][1]<<endl;
	}*/
	memset(color,0,sizeof(color));
	color[count]=1;
 
	for(i=0;i<=t;i++)//染色开始 
	{
		if(s[i]=='w')
		{
			colorful(post[i][0],post[i][1],0);
		}
		else
		{
			colorful(post[i][0],post[i][1],1);
		}
	}
	/*for(i=0;i<10;i++)
	{
		cout<<color[i]<<' ';
	}*/
	int flag=0,sum,ans=0,ansl,ansr,left;
	for(i=0;i<=count;i++)//开始找最长的了,马上就结束了。 
	{
		if(color[i]==0)
		{
			if(!flag)
			left=i;
			flag=1;
			continue;
		}
		else if(color[i]==1)
		{
			if(flag)
			{
				sum=value[i]-value[left];
				flag=0;
				if(sum>ans)
				{
					ans=sum;
					ansl=value[left];
					ansr=value[i];
				}
			}
		}
	}
	printf("%d %d\n",ansl,ansr);
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值