首页 > 其他 > 详细

Color the Ball2

时间:2014-02-27 15:48:19      阅读:518      评论:0      收藏:0      [点我收藏+]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

  

 

bubuko.com,布布扣
#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\n");
        }
        else{
            printf("%d %d\n", x, y);
        }
    }
    return 0;
}
bubuko.com,布布扣

Color the Ball2,布布扣,bubuko.com

Color the Ball2

原文:http://www.cnblogs.com/littlehoom/p/3569904.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!