BZOJ 2298 problem a

Posted on 2017-03-13 19:22  ziliuziliu  阅读(145)  评论(0编辑  收藏  举报

线段覆盖。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#define maxn 100050
using namespace std;
int n,a,b,f[maxn];
struct seg
{
    int l,r;
    seg (int l,int r):l(l),r(r) {}
    seg () {}
    friend bool operator < (const seg &x,const seg &y)
    {
        return (x.l<y.l || ((x.l==y.l) && (x.r<y.r)));
    }
    friend bool operator > (const seg &x,const seg &y)
    {
        return (x.l>y.l || ((x.l==y.l) && (x.r>y.r)));
    }
}s[maxn];
vector <int> v[maxn];
map <seg,int> mp;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        scanf("%d%d",&a,&b);
        if (b+1>n-a || a>=n || b>=n) continue;
        else {s[i]=seg(b+1,n-a);v[n-a].push_back(i);mp[s[i]]++;}
    }
    for (int i=1;i<=n;i++)
    {
        f[i]=f[i-1];
        for (int j=0;j<v[i].size();j++) f[i]=max(f[i],f[s[v[i][j]].l-1]+min(i-s[v[i][j]].l+1,mp[s[v[i][j]]]));
    }
    printf("%d\n",n-f[n]);
    return 0;
}