Luogu_P2519 [HAOI2011] problem a [DP] explanations

Questions surface: https://www.luogu.org/problem/P251 9

Face really simple question .

This man set his previous ranking number plus one.

Then we can know the number of n-bi-ai is tied.

Let l ai + 1 Let r n-bi.

So is the number of parallel r-l + 1.

So naturally it is seeking a number of disjoint segments, so that their value and maximum.

Fi is set to the maximum value and i.

f[i]=max(f[i-1],f[now]+v[i]);

code show as below:

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
struct node{
    int l,r,v;
}a[maxn],b[maxn];
int cnt,n,tot,f[maxn];
inline bool cmp1(node x,node y){return x.l==y.l ? x.r<y.r : x.l<y.l ;}
inline bool cmp2(node x,node y){return x.r==y.r ? x.l<y.l : x.r<y.r ;}
inline int erf(int l,int r,int d){
    while(l<=r){
        int mid=(l+r)>>1;
        if(a[mid].r<d) l=mid+1;
        else r=mid-1;
    }
    return r;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x,y;scanf("%d%d",&x,&y);
        a[i].l=x+1;a[i].r=n-y;
    }
    sort(a+1,a+1+n,cmp1);
    for(int i=1;i<=n;i++) if(a[i].l<=a[i].r) b[++tot]=a[i];
    for(int i=1;i<=tot;i++)
        if(i==1 || b[i].l !=b[i-1].l || b[i].r!=b[i-1].r) a[++cnt]=b[i],a[cnt].v=1;
        else if(a[cnt].v<a[cnt].r-a[cnt].l+1) a[cnt].v++;
    sort(a+1,a+1+cnt,cmp2);
    f[1]=a[1].v;
    for(int i=2;i<=cnt;i++){
        int now=erf(1,i-1,a[i].l);
        f[i]=max(f[i-1],f[now]+a[i].v);
    }
    printf("%d\n",n-f[cnt]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/ChrisKKK/p/11589023.html