Color the Ball[HDU1199]
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]的更多相关文章
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 线段树--Color the ball(多次染色问题)
K - Color the ball Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu 1199 Color the Ball
http://acm.hdu.edu.cn/showproblem.php?pid=1199 Color the Ball Time Limit: 2000/1000 MS (Java/Others) ...
- Color the ball HDOJ--1556
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdoj 1556 Color the ball【线段树区间更新】
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1199 Color the Ball(离散化线段树)
Color the Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Color the ball(树状数组+线段树+二分)
Color the ball Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
随机推荐
- scrollview里面多张图片,每张都能放大缩小
http://blog.sina.com.cn/s/blog_5d68044001018s1n.html scrollview里面多张图片,每张都能放大缩小 - (void)viewDidLoad{ ...
- Stanford机器学习---第五讲. 神经网络的学习 Neural Networks learning
原文 http://blog.csdn.net/abcjennifer/article/details/7758797 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...
- Linux 命令行生成随机密码的十种方法
Linux操作系统的一大优点是对于同样一件事情,你可以使用高达数百种方法来实现它.例如,你可以通过数十种方法来生成随机密码.本文将介绍生成随机密码的十种方法.这些方法均收集于Command-Line ...
- django-cms 代码研究(六)plugin的深入分析
示例代码: https://github.com/divio/djangocms-picture 以上一个图片的插件,安装后可在页面中添加图片,效果如下图: 以此为切入点,分析plugin的逻辑: 分 ...
- JS匿名函数的理解
js匿名函数的代码如下:(function(){ // 这里忽略jQuery 所有实现 })(); 半年前初次接触jQuery 的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的 ...
- 用JAVA代码实现验证邮箱地址是否符合
public class Test{ public static void main(String[] args){ Test t = new Test(); String email = " ...
- (int),Int32.Parse() 和 Convert.toInt32() 的区别
在 C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别? int 关键字表示一种整型,是32位的,它的 .NET Framework 类型为 S ...
- jqGrid 各种参数 详解
JQGrid JQGrid是一个在jquery基础上做的一个表格控件,以ajax的方式和服务器端通信. JQGrid Demo 是一个在线的演示项目.在这里,可以知道jqgrid可以做什么事情. 下面 ...
- 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 ...
- Android dp px转化公式
// DisplayMetrics metrics = getResources().getDisplayMetrics(); // int statusBarHeight = (int) Math. ...