poj 3184 dp

來源:互聯網
上載者:User

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxl=1e6+9,maxn=1e3+9;int dp[maxl],que[maxl],top,front;int n,m,l,r;bool use[maxl];struct D{    int l,r;    bool operator <(const D & xx) const    {        return l<xx.l;    }}data[maxn];void init(){    memset(use,0,sizeof(use));    memset(dp,50,sizeof(dp));    dp[0]=top=0;    front=1;}inline void push(int que[],int &front,int &top,int t){    while(top>=front&&dp[t-l]<dp[que[top]]) top--;    que[++top]=t-l;}inline void pop(int que[],int &front,int &top,int t){    while(front<=top&&t-que[front]>r) front++;}int main(){//    freopen("in.txt","r",stdin);    while(scanf("%d %d",&n,&m)!=EOF)    {        init();        scanf("%d %d",&l,&r);        for(int i=1;i<=n;i++) scanf("%d %d",&data[i].l,&data[i].r);        sort(data+1,data+1+n);        for(int i=1,to=0;i<=n;i++)        {            int tmp=max(to,data[i].l+1);            for(int j=tmp;j<data[i].r;j++)            use[j]=1;            to=max(to,data[i].r-1);        }        l<<=1,r<<=1;        for(int i=l;i<=m;i+=2)        {            if(!use[i-l])            push(que,front,top,i);            pop(que,front,top,i);            if(front<=top&&!use[i])            dp[i]=dp[que[front]]+1;        }        if(dp[m]<1e7)        cout<<dp[m]<<endl;        else        cout<<-1<<endl;    }    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.