您的位置:首页 > 大数据 > 人工智能

URAL 1019 Line Painting(解题报告)

2012-09-04 13:24 330 查看
Description

The segment of numerical axis from 0 to 109 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’, ai, bi, ci 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 < 109.

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

inputoutput
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: