hdu1199:color the ball(離散化)



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
3 1 4 w 8 11 w 3 5 b
 

Sample Output
8 11
 

Author
ZHOU, Kai
 

Source
 

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;
}

發佈了41 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章