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 ...
随机推荐
- 中位数与第K小元素
算法实际上是模仿快速排序算法设计出来的,其基本思想也是对输入数组进行递归划分,与快速排序不同的是,它只对划分出来的子数组之一进行递归处理: int randompartition(int a[],in ...
- postgresql 函数demo
create or replace function refresh_product_usage() returns void as $$ declare rec record; sub_rec re ...
- linux ls正则表达式
ls就是默认排序的. 所以: ls只支持通配符,不支持正则,所以单纯用ls是不能实现的. 一些正则过滤操作需要结合支持正则的命令如grep.sed或awk. 例如:ls | grep "[0 ...
- java中四种阶乘的计算
package com.zf.s2;//创建一个包 import java.math.BigInteger;//导入类 import java.util.ArrayList; import jav ...
- 把.pvr.ccz文件转换成png
我用的是一个万能转换法,原理是先用CCSprite加载.pvr.ccz,然后把它绘制到一个CCRenderTexture上,然后再保存到文件里.这方法其实不只.pvr.ccz文件,其他所有能被coco ...
- Android 中的Force Close
今天写程序时遇到一个问题,领导希望在点击了setting里的force close 后,程序依然能够响应以前用alarmManager注册的receiver. 在网上看到了一些文章,写的是如何建立一个 ...
- Cocos2d 中的Sprite大小调整问题
以前用UIImageView,比如 UIImageView *view = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"b ...
- Android activity_main.xml删除边缘距离,充满屏幕
删除android:paddingBottom.android:paddingLeft.android:paddingRight和android:paddingTop四个属性
- 信与信封问题(codevs 1222)
题目描述 Description John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出.但是,第二天John的儿子Small John将这n封信都拿出了信封.不幸的是,Small Joh ...
- operator new与new operator的区别
原文地址:http://www.cnblogs.com/jamesmile/archive/2010/04/17/1714311.html,在此感谢 C++中的operator new与new ope ...