• Color the Ball2


    Color the Ball
    
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4028    Accepted Submission(s): 999
    
    
    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
    

      

    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    struct node{
        int beg, en;
    }s[8000];
    int len;//维持线段长度
    bool cmp(node a, node b){
        if (a.beg != b.beg)
            return a.beg < b.beg;//从小到大排有利于寻找可连接的段
        else
            return a.en>b.en;
    }
    void Process(int h, int e, char c){
        //只对将白涂黑进行处理,总共有4种情况
        //离散区间为[ ]
        if (c == 'w'){
            s[len].beg = h;
            s[len++].en = e;
        }
        else{
            for (int i = 0; i < len; i++){
                if (s[i].beg >= h&&s[i].en <= e){
                    s[i].beg = s[i].en = -1;
                }
                else if (s[i].beg >= h&&s[i].en >= e&&s[i].beg <= e){
                    s[i].beg = e + 1;
                }
                else if (s[i].beg <= h&&s[i].en >= e){
                    s[len].en = s[i].en;//先这一步,否则会被覆盖,^v^
                    s[i].en = h - 1;
                    s[len++].beg = e + 1;
                }
                else if (s[i].beg <= h&&s[i].en >= h&&s[i].en<=e){
                    s[i].en = h - 1;
                }
            }
        }
    }
    int main()
    {
        int cnt, n, h, e, x, y;
        char c;
        while (scanf("%d", &n) != -1){
            memset(s, 0, sizeof(s));
            len = 0;
            for (int i = 0; i < n; i++){
                scanf("%d %d %c", &h, &e, &c);
                Process(h, e, c);
            }
            cnt = h = e = -1;
            sort(s, s + len, cmp);
            for (int i = 0; i < len; i++){
                if (s[i].beg != -1){
                    if (s[i].beg <= e + 1){
                        e = max(e, s[i].en);
                    }
                    else{
                        h = s[i].beg;
                        e = s[i].en;
                    }
                    if (cnt < e - h + 1){
                        cnt = e - h + 1;
                        x = h; y = e;
                    }
                }
            }
            if (cnt == -1){
                printf("Oh, my god
    ");
            }
            else{
                printf("%d %d
    ", x, y);
            }
        }
        return 0;
    }
    世上无难事,只要肯登攀。
  • 相关阅读:
    CSS动画DAY01
    第5章到8章小结
    实验9 根据材料编程
    实验5 编写、调试具有多个段的程序
    实验4 [bx]和loop的使用
    实验3 编程、编译、连接、跟踪
    第3章 寄存器(内存访问)小结
    实验2 用机器指令和汇编指令编程
    实验1 查看CPU和内存,用机器指令和汇编指令编程
    字符串生成及加密
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3569904.html
Copyright © 2020-2023  润新知