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 ...
随机推荐
- 删除重复的字符(给一个字符串,删除连续重复的字符,要求时间复杂度为O(1)……)
// singal.cpp : Defines the entry point for the console application.// #include "stdafx.h" ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- OpenGL实现三维立方体交互
http://yunpan.cn/cs62JgxTNs98C (提取码:668e)
- 【Python】使用 sphinx 制作简洁而又美观的文档
参考资料: http://zh-sphinx-doc.readthedocs.io/en/latest/tutorial.html http://avnpc.com/pages/writing-bes ...
- x:Name标记特性与Name属性
本文转载自silvergingko的专栏 在Xaml中定义了一个元素后,如果后面要使用该元素,则必须为该元素定义一个元素名称,在随后的Xaml中,通过元素名称来使用该元素. 在Xaml中,元素的名称定 ...
- Lowest Common Ancestor
Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes ...
- 如何给spine骨骼动画挂载粒子特效
目的是要把粒子挂载到骨骼动画的某个一个部件上,其实最主要是找对位置. 预览效果,左手红火,右手蓝火,很炫吧:) //init bool HelloWorld::init() { /////////// ...
- After Effects的4种抠像插件比较分析
前景 背景 1.keylight(1.2) 2.Primatee Keyer Pro4.0 3.Zbig [边界生硬] 4.Power Matte v2 [速度很慢,边界生硬]
- Java for LeetCode 201 Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- codeforces A. Sereja and Bottles 解题报告
题目链接:http://codeforces.com/problemset/problem/315/A 题目意思:有n个soda bottles,随后给出这n个soda bottles的信息.已知第 ...