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. laravel/laravel和laravel/framework有何区别?

    在安装laravel的时候,我们一般是download github上的laravel/laravel,随后执行composer install,在这个过程中,你会发现composer其中的一项工作是 ...

  2. LA 3695 Distant Galaxy

    给出n个点的坐标(坐标均为正数),求最多有多少点能同在一个矩形的边界上. 题解里是构造了这样的几个数组,图中表示的很明白了. 首先枚举两条水平线,然后left[i]表示竖线i左边位于水平线上的点,on ...

  3. Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence

    Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence 如下代码: for (ind ...

  4. myisam 与innodb的区别

    myisam 与innodb的区别 frm结构 fri索引 frd数据 innodb:一个表一个文件:frm文件 所有的innodb表,都使用表空间储存, 数据和索引的保存文件不同,myisam 分开 ...

  5. 不要随随便便的distinct和order by

    相关查询非常慢,通过程序拿到了相关sqlexplainexplain SELECT DISTINCT(o.orders_id), o.oa_order_id, customers_email_addr ...

  6. svn sc create 命令行创建服务自启动

    今天重装了系统,Subversion服务也得重装.照例输入代码: sc create svnserve binpath="d:\PortableApps\svn\svnserve.exe - ...

  7. php 序列化(serialize)格式详解

    1.前言 PHP (从 PHP 3.05 开始)为保存对象提供了一组序列化和反序列化的函数:serialize.unserialize.不过在 PHP 手册中对这两个函数的说明仅限于如何使用,而对序列 ...

  8. hdu 1023(java实现进度计算)

    题意:就是问你火车出战的方案数. 分析:卡特兰数的模板题,递推公式:a[n]=a[n-1]*(4*n-2)/(n+1). java代码实现: import java.util.*; import ja ...

  9. CXF之九 WS-Security

    Webservice 的安全 Webservice为作为方便的服务被用广大领域使用的同时,也成为了黑客们的美食.在这里,本文将就目前对Webservice安全所能做的改进做简单介绍.在Webservi ...

  10. 关于COUNT STOPKEY的工作机制(转载)

    SQL> select rownum rn ,a.* from cnmir.ew_auctions a where rownum<50000; Execution Plan-------- ...