$ Poj2376 \ Poj3171 \ Cleaning \ Shifts $ $ optimization of the DP data structure $

Capacity $ $     $ $ AcWing

$Description$

 

 

$Sol$

Covering issues that range $ qwq! $

1. $ DP $ do!

$ F [i] $ denotes the number of calves cover most $ [1, i] $ required

Cattle working time from small to large, sequentially scanning these cows in accordance with the right end (end time) r [i] is the set of current $ I $ cattle, is:

$F[r[i]]=(min_{l_i-1<=j<r_i}^{ }F[j])+1$

$ F [0] = 0 $, the rest is initialized to + ∞

Algorithms guide training: Interval most value problem with the modified segment tree $ ??? $ $ $ !!!

2. greedy do!

Than $ DP $ better write more, $ DP $ wrote the segment tree ...

First, sort all of the sections in ascending order according to a right end points, sequentially scanning these intervals.

In short intervals in order to answer exists in the queue, if the current range can replace the tail section to delete the tail, repeat cycle ($ while $), and then adding the current interval range

So how is called an alternative, that is, the current range of the left point less than or equal to the team right before a section of the tail points plus $ 1 $ on legitimate, thus ensuring that every point is covered to satisfy the above conditions, and the current range than the right end point of the tail section larger (or the like), so the current interval must better.

Details: from left to right during the scanning interval, if the right end of the tail section had $> = T $, then it must be out of the loop, otherwise the answer may be too large

 

$Code$

#include<bits/stdc++.h>
#define il inline
#define Rg register
#define go(i,a,b) for(Rg int i=a;i<=b;i++)
#define yes(i,a,b) for(Rg int i=a;i>=b;i--)
using namespace std;
il int read()
{
    int x=0,y=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    return x*y;
}
int n,t,p,minl=1e9,maxr;
struct node{int l,r,k;}a[25010],as[25010];
il bool cmp(node x,node y){if(x.r==y.r)return x.l<y.l;return x.r<y.r;}
int main()
{
    n=read(),t=read();
    go(i,1,n){a[i]=(node){read(),read()};minl=min(minl,a[i].l),maxr=max(maxr,a[i].r);}
    if(minl>1 || maxr<t){printf("-1\n");return 0;}
    sort(a+1,a+n+1,cmp);
    bool fl=0;
    go(i,1,n)
    {
        if(as[p].r>=t)break;
        while(p && as[p].k>=a[i].l)p--;
        if(a[i].l<=as[p].r+1){as[++p]=a[i];as[p].k=as[p-1].r+1;}
    }
    if(as[p].r>=t)printf("%d\n",p);
    else printf("-1");
    return 0;
}

 

 

 

$Poj$    $AcWing$

Guess you like

Origin www.cnblogs.com/forward777/p/11264398.html