hdu1199:color the ball(离散化)

来源:互联网 发布:淘宝泻油汤真相 编辑:程序博客网 时间:2024/06/06 13:57


Color the Ball

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4244    Accepted Submission(s): 1055


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
31 4 w8 11 w3 5 b
 

Sample Output
8 11
 

Author
ZHOU, Kai
 

Source
ZOJ Monthly, February 2005
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1166 1542 1754 1543 1394
离散化(只考虑我们需要的值) 
 把涂上黑色看成是对白色区间的更新。求出所有存在的白色区间,再遍历一下。
把所有可以相连的白色区间连在一起(之前要排下序),并求可能的最大值。
下面是我的代码。
#include<iostream>#include<algorithm>using namespace std;const int MAXN = 3001;struct node{    int left, right;    int iswhite;}nd[MAXN];int cnt;int max(int a,int b){return a>b?a:b;}bool cmp(const node &n1,const  node &n2){    return n1.left < n2.left || (n1.left == n2.left&&n1.right < n2.right);}void update(int l,int r,char c){    if (c == 'w'){ //加上这个涂成白色的区间        nd[cnt].left = l;        nd[cnt].right = r;        nd[cnt].iswhite = 1;        cnt++;    }    else if (c == 'b'){ //对所有现有涂成白色区间的更新。        int i;        int k = cnt;        for (i = 0; i < k; i++){            if (nd[i].left < l&&nd[i].right > r){                 nd[cnt].left = r + 1;                nd[cnt].right = nd[i].right;                nd[cnt].iswhite = 1;                nd[i].right = l-1;                cnt++;            }            else if (nd[i].left >= l&&nd[i].right <= r){                nd[i].iswhite = 0;            }            else if (nd[i].left<l&&nd[i].right>=l){                nd[i].right = l - 1;            }            else if (nd[i].left<=r&&nd[i].right > r){                nd[i].left = r + 1;            }        }    }}void getRes(){    int i, j;int l,r;int len=0; //白色区间的长度l=r=-1;    for (i = 1; i < cnt; i++){//printf("%d %d",nd[i-1].left,nd[i-1].right);        if (nd[i].iswhite == 1 && nd[i - 1].iswhite == 1){            if(nd[i].left >= nd[i - 1].left&&nd[i].left <= nd[i-1].right){                nd[i].left = nd[i - 1].left;                if (nd[i].right<nd[i-1].right){                     nd[i].right = nd[i - 1].right;                }nd[i-1].iswhite=0;int a=nd[i].right-nd[i].left+1;if(a>len){l=nd[i].left;r=nd[i].right;len=a;}}else if(nd[i].left>nd[i-1].right){int a=nd[i].right-nd[i].left+1;int b=nd[i-1].right-nd[i-1].left+1;if(a<=b){if(b>len){l=nd[i-1].left;r=nd[i-1].right;nd[i]=nd[i-1];nd[i-1].iswhite=0;len=b;}}else{if(a>len){l=nd[i].left;r=nd[i].right;len=a;}}}        }        else if (nd[i - 1].iswhite == 1 && nd[i].iswhite == 0){            nd[i] = nd[i - 1];nd[i-1].iswhite=0;        }else if(nd[i].iswhite==1&&nd[i-1].iswhite==0){int a=nd[i].right-nd[i].left+1;if(a>len){l=nd[i].left;r=nd[i].right;len=a;}}    }    if (len==0){        printf("Oh, my god\n");    }       else         printf("%d %d\n", l, r);}int main(){    int n;    int a, b;    char c;    freopen("in.txt", "r", stdin);    while (~scanf("%d", &n)){        cnt = 0;        memset(nd, 0, sizeof(nd));        while (n--){            scanf("%d %d %c", &a, &b,&c);            update(a, b,c);        }        sort(nd, nd + cnt, cmp);        getRes();    }    return 0;}

0 0