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. HDU 3537 (博弈 翻硬币) Daizhenyang's Coin

    可以参考Thomas S. Ferguson的<Game Theory>,网上的博客大多也是根据这个翻译过来的,第五章讲了很多关于翻硬币的博弈. 这种博弈属于Mock Turtles,它的 ...

  2. UVa 11019 (AC自动机 二维模式串匹配) Matrix Matcher

    就向书上说得那样,如果模式串P的第i行出现在文本串T的第r行第c列,则cnt[r-i][c]++; 还有个很棘手的问题就是模式串中可能会有相同的串,所以用repr[i]来记录第i个模式串P[i]第一次 ...

  3. swift2.0 Cannot assign a value of type '[CFString]' to a value of type '[String]'

    Cannot assign a value of type '[CFString]' to a value of type '[String]' 代码示例如下: picker.mediaTypes = ...

  4. Activiti 多个并发子流程的应用

    多个部门发起资金计划,最后统一到财务部审批,每个部门发起资金计划是一个子流程,财务部审批是多个部门的计划同时审批,审批完成后,再提交上级领导审批. 流程如下: 要解决以上问题,需要实现多个子流程并行处 ...

  5. 【英语】Bingo口语笔记(75) - 元音辅音的辨读

  6. TCP握手

    1.TCP的三次握手四次挥手 第一次握手:Client将标志位SYN置为1,随机产生一个值seq=J,并将该数据包发送给Server,Client进入SYN_SENT状态,等待Server确认. 第二 ...

  7. Excel 之查找与替换

    Excel查找与替换 1,一旦学会查找,替换就简单了. 2,查找下面有一个选项,里面有对查找的范围进行限制 3,你可以选中一个区域,然后再查找,查找只会在你选择的区域里面进行 4,查找只能找特定值,而 ...

  8. Heritrix源码分析(十二) Heritrix的控制中心(大脑)CrawlController(一)(转)

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

  9. 嵌入式 H264中的SPS、PPS提取与作用

    使用RTP传输H264的时候,需要用到sdp协议描述,其中有两项:Sequence Parameter Sets (SPS) 和Picture Parameter Set (PPS)需要用到,那么这两 ...

  10. ERROR:The requested URL could not be retrieved解决方法

    ERROR 错误 The requested URL could not be retrieved 您所请求的网址(URL)无法获取 While trying to retrieve the URL: ...