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. some resource favor

    http://www.moxiemanager.com/getit/ : picture file manage with blur 可以和Tinymce结合使用完美实现WYSIWYG的效果 http ...

  2. 总结css兼容问题

    目前主流浏览器的兼容性做的都比较好了,本文主要针对IE6,7的不兼容问题进行解决. 1.有浮动存在时,计算一定要精确,不要让内容的宽高超出我们所设置的宽高,IE6下,内容会撑开设置好的高度. 解决方法 ...

  3. Hadoop实战课程

    Hadoop生态系统配置Hadoop运行环境Hadoop系统架构HDFS分布式文件系统MapReduce分布式计算(MapReduce项目实战)使用脚本语言Pig(Pig项目实战)数据仓库工具Hive ...

  4. nanakon

    1.安装python pip3 install tornado pip3 install pymysql pip3 install qiniu pip3 install pillow 2.安装mysq ...

  5. a标签的href劫持,做判断后在跳转

    $.ajax({ type: "POST", url: "/resource/logincheck", data: {id: id}, success: fun ...

  6. OpenGL学习之路(三)

    1 引子 这些天公司一次次的软件发布节点忙的博主不可开交,另外还有其它的一些事也占用了很多时间.现在坐在电脑前,在很安静的环境下,与大家分享自己的OpenGL学习笔记和理解心得,感到格外舒服.这让我回 ...

  7. RequireJS进阶(三) 转

    进阶的前面两篇讲述了r.js如何通过命令行把所有的模块压缩为一个js文件或把所有的css压缩为一个css文件.其中包括一些压缩配置参数的使用. 但以上两种方式有几个问题 1.通过命令手动配置压缩选项显 ...

  8. Fragment监听返回键

    首先创建一个抽象类BackHandledFragment,该类有一个抽象方法onBackPressed(),所有BackHandledFragment的子类在onBackPressed方法中处理各自对 ...

  9. nginx upstream的几种配备方式

     nginx upstream的几种配置方式 nginx 的upstream目前支持4种方式的分配 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器 ,如果后端服务器down掉,能自动剔 ...

  10. Delphi MDI程序 父窗体如何调用当前活动子窗体的函数/过程

    一个MDI文本文件编辑器打开了N个子窗体子窗体的.pas文件有一些public的过程和函数我想在父窗体调用当前活动的子窗体函数我用Self.ActiveChildForm无法调用直接frmEdit.x ...