HDU 1199 Color the Ball(線段切割 離散化思想)

來源:互聯網
上載者:User

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1199

這個題目開始一看就知道用線段樹加離散化來解,可是好長時間沒寫線段樹了,一時間不知道離散化用線段樹怎麼搞定

於是就用一個網上稱是切割線段的類似類比方法,感覺這個方法比較容易想,但是寫的過程中每一步到底是加一還是減

一都要想好,表示很煩人,開始寫的時候其實也挺矛盾,分析下複雜度如果專門挑那些奇怪的測試資料可能會逾時,還

是寫出來了,果然不出所料wa了幾次,但是後來46ms通過可能是資料的問題吧,至於離散化思想我倒是覺得沒怎麼體

現,反正寫出來的代碼感覺很噁心了!

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define maxn 2500struct point{    int l;    int r;}po[maxn];int pos;int n;int black(int a,int b){    int p=pos;    for(int i=0;i<p;i++)    {       if(a>po[i].r || b<po[i].l)       continue;       if(a<=po[i].l && b>=po[i].r)       po[i].l=0,po[i].r=-1;       else if(a>po[i].l && b<po[i].r)       po[pos].l=b+1,po[pos].r=po[i].r,po[i].r=a-1,pos++;       else if(a<=po[i].l && b<po[i].r)       po[i].l=b+1;       else if(a>po[i].l && b>=po[i].r)       po[i].r=a-1;    }    return 0;}bool cmp(const point &a,const point &b){    if(a.l==b.l)    return a.r < b.r;    return a.l < b.l;}int main(){    int i,j,k,a,b;    char ch;    int ans,left,now,my_pos,re_pos;    while(scanf("%d",&n)!=EOF)    {        ans=0;        pos=0;        left=0;        for(i=0;i<n;i++)        {        scanf("%d%d",&a,&b);        getchar();        scanf("%c",&ch);        if(ch=='w')        {            po[pos].l=a;            po[pos].r=b;            pos++;        }        else        {            black(a,b);        }        }        sort(po,po+pos,cmp);        //for(i=0;i<pos;i++)        //printf("%d %d \n",po[i].l,po[i].r);        if(pos==0)        {            printf("Oh, my god\n");            continue;        }        i=0;        while(po[i].r==-1)        i++;        if(i<pos)        {        now=po[i].r-po[i].l+1;        left=po[i].r;        my_pos=left;        }        else        ans=0,now=0;        for(i;i<pos;i++)        {            if(po[i].l <= left+1)            {                if(po[i].r <= left)                continue;                else                now+=po[i].r-left,my_pos=po[i].r,left=po[i].r;            }            else            {                if(ans < now)                ans=now,re_pos=my_pos;                now=po[i].r-po[i].l+1;                my_pos=po[i].r,left=po[i].r;            }        }         if(ans < now)            ans=now,re_pos=my_pos;        if(ans==0)        {            printf("Oh, my god\n");            continue;        }        printf("%d %d\n",re_pos-ans+1,re_pos);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.