POJ 2373 Dividing the Path

題意:長度爲L的區間,有N頭牛有各自喜歡活動的區間(Si,Ei),噴水的噴頭只能安裝在整數點上,且它能噴到的半徑在[A,B]之間,現在要求:用最少的噴水頭不交叉的覆蓋整個L,並且滿足每頭牛喜歡活動的區間裏只有一個噴頭再噴。

題解:線段樹優化DP,dp[i]代表噴完前i個地方所需要的最少噴頭數,那麼dp[i]=min(dp[j]+1),i-2b<=j<=i-2a,用線段樹記錄[i-2b,i-2a]的最小值就行了。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int L=1000005,inf=0x3f3f3f3f;
bool ck[L];
struct Line
{
    int left,right,val;
}line[L*4];
void build(int now,int left,int right)
{
    line[now].left=left;
    line[now].right=right;
    line[now].val=inf;
    if(left==right)return;
    build(now*2,left,(left+right)>>1);
    build(now*2+1,(left+right)/2+1,right);
}
void updata(int now,int pos,int val)
{
    int ll=line[now].left,rr=line[now].right,mid;
    if(ll==pos&&rr==pos)
    {
        line[now].val=min(line[now].val,val);
        return;
    }
    mid=(ll+rr)>>1;
    if(pos<=mid)
        updata(now*2,pos,val);
    else
        updata(now*2+1,pos,val);
    line[now].val=min(line[now*2].val,line[now*2+1].val);
}
int getmin(int now,int left,int right)
{
    int ll=line[now].left,rr=line[now].right,mid;
    if(ll==left&&rr==right)
        return line[now].val;
    mid=(ll+rr)>>1;
    if(right<=mid)
        return getmin(now*2,left,right);
    else if(left>mid)
        return getmin(now*2+1,left,right);
    else
        return min(getmin(now*2,left,mid),getmin(now*2+1,mid+1,right));
}
int main()
{
    int n,len,a,b,ans;
    while(scanf("%d%d",&n,&len)!=EOF)
    {
        build(1,0,len/2);
        scanf("%d%d",&a,&b);
        memset(ck,true,sizeof(ck));
        for(int i=0,x,y;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            memset(ck+x+1,false,(y-x-1)*sizeof(bool));
        }
        updata(1,0,0);
        for(int i=a*2;i<=len;i+=2)
        {
            if(ck[i])
            {
                int x=i/2-b,y=i/2-a,tp;
                x=max(0,x);
                tp=getmin(1,x,y);
                if(tp==inf)
                    tp--;
                updata(1,i/2,tp+1);
            }
        }
        ans=getmin(1,len/2,len/2);
        printf("%d\n",ans<inf?ans:-1);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章