zoukankan      html  css  js  c++  java
  • hdu 1199 Color the Ball

    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 = 0;
            memset(G, 0, sizeof(G));
            memset(node, 0, 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] = 1;
                else
                    for(i = a ; i <= b ; i++)
                        G[i] = 0;
            }
            for(i = 1 ; i < Max ; i++)
            {
                if(G[i] == 1)
                {
                    f = 1;
                    break;
                }
            }
            if(f == 0)
            {
                printf("Oh, my god
    ");
                continue;
            }
            for(i = 1 ; i <= Max ; i++)
            {
                if(G[i] == 1)
                {
                    node[j].ans++;
                    if(k == 0)
                    {
                        node[j].x = i;
                        k = 1;
                    }
                }
                if(G[i] == 0 && k == 1)
                {
                    node[j].y = i - 1;
                    k = 0;
                    j++;
                }
                if(G[i] == 1 && i == Max && k == 1)
                {
                    node[j].y = i;
                    k = 0;
                    j++;
                }
            }
            qsort(node, j, sizeof(node[0]), cmp);
            printf("%d %d
    ", node[0].x, node[0].y);
        }
        return 0;
    }
    
    /*
    3
    1 4 w
    8 11 w
    3 5 b
    4
    1 4 w
    8 11 w
    3 5 b
    6 8 w*/
  • 相关阅读:
    CSS3 非线性时间函数应用
    CSS3 animation动画与transition过渡比较
    JS 数组高阶函数 map、reduce、filter、sort等
    访问控制列表-ACL匹配规则
    C 函数——Day04
    让你的wordpress在新窗口打开链接
    EFI、UEFI、MBR、GPT的区别
    C 输入 & 输出——Day03
    C 数据类型——Day02
    博客园代码高亮
  • 原文地址:https://www.cnblogs.com/qq2424260747/p/4787103.html
Copyright © 2011-2022 走看看