Color the Ball

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3502    Accepted Submission(s): 863

Problem 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

Author
ZHOU, Kai

Source
ZOJ Monthly, February 2005

Recommend
Ignatius.L

First we should know which part is white in the end.The range of the balls' coordinate is too large for a boolean array.Fortunately ,there is a similar problem in USACO,which could be solved by floating method.Here,let each segment whose color is white float to top,and it is divide when it bump into a black one.Then the part remain is a white segment in the end.
Sort the white segment by their begin point.For the segment from 2 to T,combine it with its previous one if they have common.Then the longest element should be the answer.

#include<stdio.h>
#include<string.h>
class edge
{
public:
int l,r;
};
int N,T;
int a[2025],b[2025];
char col[2025];
edge E[1000000];
int Max(int x,int y)
{
return x>y ? x:y;
}
int Min(int x,int y)
{
return x<y ? x:y;
}
void dfs(int l,int r,int dep)
{
if (dep==N)
{
T++;
E[T].l=l;
E[T].r=r;
return;
}
if (col[dep+1]=='w') dfs(l,r,dep+1);
else
{
if (l<a[dep+1]) dfs(l,Min(r,a[dep+1]-1),dep+1);
if (b[dep+1]<r) dfs(Max(b[dep+1]+1,l),r,dep+1);
}
}
void qsort(int l,int r)
{
int i=l,j=r,x=E[(l+r)>>1].l;
do
{
while (E[i].l<x) i++;
while (x<E[j].l) j--;
if (i<=j)
{
edge tmp=E[i];
E[i]=E[j];
E[j]=tmp;
i++;
j--;
}
}
while (i<=j);
if (i<r) qsort(i,r);
if (l<j) qsort(l,j);
}
int main()
{
while (scanf("%d",&N)!=EOF)
{
int i;
for (i=1;i<=N;i++)
{
scanf("%d %d %c",&a[i],&b[i],&col[i]);
if (a[i]>b[i])
{
int t=a[i];
a[i]=b[i];
b[i]=t;
}
}
T=0;
for (i=1;i<=N;i++)
if (col[i]=='w') dfs(a[i],b[i],i);
qsort(1,T);
for (i=2;i<=T;i++)
if (E[i-1].l<=E[i].l && E[i].l<=E[i-1].r+1)
{
if (E[i-1].l<E[i].l) E[i].l=E[i-1].l;
if (E[i].r<E[i-1].r) E[i].r=E[i-1].r;
}
if (T==0)
{
printf("Oh, my god\n");
continue;
}
int MAX=0;
for (i=1;i<=T;i++)
if (E[i].r-E[i].l+1>MAX) MAX=E[i].r-E[i].l+1;
for (i=1;i<=T;i++)
if (E[i].r-E[i].l+1==MAX)
{
printf("%d %d\n",E[i].l,E[i].r);
break;
}
}
return 0;
}

Color the Ball[HDU1199]的更多相关文章

  1. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  2. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. 线段树--Color the ball(多次染色问题)

    K - Color the ball Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  5. hdu 1199 Color the Ball

    http://acm.hdu.edu.cn/showproblem.php?pid=1199 Color the Ball Time Limit: 2000/1000 MS (Java/Others) ...

  6. Color the ball HDOJ--1556

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hdoj 1556 Color the ball【线段树区间更新】

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. hdu 1199 Color the Ball(离散化线段树)

    Color the Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. Color the ball(树状数组+线段树+二分)

    Color the ball Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

随机推荐

  1. Unity 3D学习之 Prime31 Game Center插件用法

    http://momowing.diandian.com/post/2012-11-08/40041806328 It's my life~: 为app 连入Game Center 功能而困扰的朋友们 ...

  2. 使用nginx的proxy_cache做网站缓存

    为什么要做web cache,我想大家最主要的是解决流量的压力.随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力.与此同时某些网站的页面 ...

  3. MyBatis3: There is no getter for property named 'code' in 'class java.lang.String'

    mybatis3  : mysql文如下,传入参数为string类型时‘preCode’,运行报错为:There is no getter for property named 'preCode' i ...

  4. 六间房 去掉水印的方法 绕过游客VIP限制

    firefox 40 + Adblock Plus 2.6.9.1 + Execute JS 0.2.4.1 Adblock Plus 过滤规则里添加 ------------------------ ...

  5. 前台js分页,自己手写逻辑

    js代码如下: //设置分页 var pageSize = 10; //设置一次显示多少页 var pageLimit = 5; $(function(){ //查询所有内容 $.post(ctx + ...

  6. 高阶函数复习:利用reduce和map把字符串转为数字

    复习高阶函数的时候,有一道题想了半天解不出来.看了下别人的解法,发现学习编程,思维真的很重要. 习题: 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数1 ...

  7. Fedora 启动sshd服务:

    .先确认是否已安装ssh服务: [root@localhost ~]# rpm -qa | grep openssh-server openssh-server-.3p1-.fc12.i686 (这行 ...

  8. etc目录名字的意思---挖Linux中的古老缩略语

    Unix已经有35年历史了.许多人认为它开始于中世纪,这个中世纪是相对于计算机技术的产生和发展来说的.在过去的时间里,Unix和它的子分支Linux收集有许多的历史和一些完全古老的语言.在这篇技巧文章 ...

  9. 微信支付v3发布到iis时的证书问题(转)

    本文纯粹转载(原地址:微信支付v3发布到iis时的证书问题 ) 一开始报“出现了内部错误” 解决方法是 方法一 var cer = new X509Certificate(certpath, pass ...

  10. SAE上传web应用(包括使用数据库)教程详解及问题解惑

    转自:http://blog.csdn.net/baiyuliang2013/article/details/24725995 SAE上传web应用(包括使用数据库)教程详解及问题解惑: 最近由于工作 ...