http://acm.hdu.edu.cn/showproblem.php?pid=1199

Color the Ball

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

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
 

开始看到这道题以为要用到线段树会很难,仔细一想发现并不是多难,用一般方法就可以解决正常模拟一下染色,

0为黑色1位白色,然后循环统计每一段的白色个数,并将其起点和终点记录到结构体中,

通过排序找到白色个数最多的那一段,输出其起点和终点,即为该题结果

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 200010//这里注意开大点 using namespace std; struct st
{
int x, y, ans;//x表示白色部分的起点,y表示终点,ans表示这段区间的白色的数量
} node[N]; int G[N];//G[i] 0表示黑色 1表示白色 int cmp(const void *a, const void *b)
{
st *s1 = (st *)a, *s2 = (st *)b;
if(s1->ans == s2->ans)
return s1->x - s2->x;
else
return s2->ans - s1->ans;
} int main()
{
int n, a, b, i, f, k, j, Max;
char ch;
while(~scanf("%d", &n))
{
f = k = j = Max = ;
memset(G, , sizeof(G));
memset(node, , sizeof(node));
while(n--)
{
scanf("%d%d %c", &a, &b, &ch);
Max = max(max(a, b), Max);
if(ch == 'w')
for(i = a ; i <= b ; i++)
G[i] = ;
else
for(i = a ; i <= b ; i++)
G[i] = ;
}
for(i = ; i < Max ; i++)
{
if(G[i] == )
{
f = ;
break;
}
}
if(f == )
{
printf("Oh, my god\n");
continue;
}
for(i = ; i <= Max ; i++)
{
if(G[i] == )
{
node[j].ans++;
if(k == )
{
node[j].x = i;
k = ;
}
}
if(G[i] == && k == )
{
node[j].y = i - ;
k = ;
j++;
}
if(G[i] == && i == Max && k == )
{
node[j].y = i;
k = ;
j++;
}
}
qsort(node, j, sizeof(node[]), cmp);
printf("%d %d\n", node[].x, node[].y);
}
return ;
} /*
3
1 4 w
8 11 w
3 5 b
4
1 4 w
8 11 w
3 5 b
6 8 w*/

hdu 1199 Color the Ball的更多相关文章

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

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

  2. hdu 1199 Color the Ball 离散线段树

    C - Color the Ball Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  3. ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和

    题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...

  4. HDU 1199 - Color the Ball 离散化

    [题意]现在有几个球排成一排,编号从1开始,开始时所有球为黑色,现在有n(<=2000)次操作,每次操作将l[i]至r[i](均在int范围)的球凃成颜色c[i](黑色'b'或白色'w'),然后 ...

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

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

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

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

  7. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  8. HDU 1556 Color the ball (数状数组)

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

  9. 线段树(求单结点) hdu 1556 Color the ball

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

随机推荐

  1. Java面向对象编程概述

    一. 01.软件开发经历周期: 软件分析:分析问题领域,了解客户的需求 软件设计:确定软件的总体结构,把整个软件系统划分为大大小小的多个子系统,设计每个子系统的具体结构 软件编码:用选定的编程语言来编 ...

  2. LA 4725 (二分) Airport

    题意: 有W.E两个跑道,在每个时刻每个跑道的飞机都从0开始编号,而且每个时刻都有Wi和Ei架飞机到达这两个跑道.而且每个时刻只能选择一个跑道的一架飞机起飞.问如何选择才能使得飞机的最大编号最小.(每 ...

  3. BZOJ 4269 再见Xor

    线性基. 求次大值就是再异或一个线性基好了. #include<iostream> #include<cstdio> #include<cstring> #defi ...

  4. android系统掉电保护

    /************************************************************************ * android系统掉电保护 * 说明: * An ...

  5. I.MX6 ov5640 camera

    /************************************************************************ * I.MX6 ov5640 camera * 说明 ...

  6. Heritrix源码分析(八) Heritrix8个处理器(Processor)介绍(转)

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/643367       本博客已迁移到本人独立博客: http://www.yun5u ...

  7. 定制Bootstrap遇到无法下载的解决——Blob下载注意事项

    今天定制bootstrap(在这里),全部的勾都选过了,于是兴高采烈地点击“编译并下载”.等了一会儿,迅雷7跳出来了“新建下载任务”,但是它居然说这个url不合法! url像这样: blob:http ...

  8. Mysql数据库优化总结2

    说明:本文的环境为CENTOS 5.5 64 Bit /Mysql 5.1.50 简介:使用Mysql有一段时间了,期间做了不少关于Mysql优化.设计.维护的工作,这两天有时间做一下简单的总结,方便 ...

  9. [Everyday Mathematics]20150228

    试证: $$\bex \int_0^\infty \sin\sex{x^3+\frac{\pi}{4}}\rd x =\frac{\sqrt{6}+\sqrt{2}}{4}\int_0^\infty ...

  10. Effective java笔记4--方法

    一.检查参数的有效性 极大多数方法和构造函数都会对于传递给它们的参数值有某些限制. 对于公有的方法,使用Javadoc @throws标签(tag)可以使文档中记录下“一旦针对参数值的限制被违反之后将 ...