DP P1259

Posted on 2017-10-29 17:16  Murs  阅读(224)  评论(0)    收藏  举报

线性覆盖

f[i]是到每个区间的末尾能吃到的最大桶数

水题

、、、、、、、、、、、、、、、、、、、、、、、、、、

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

int temp;

int n,f[2200];

int maxx=0;

struct mmm

{

    int one,another;

    int...

线性覆盖;

f[i]是到每个区间的末尾能吃到的最大桶数

水题

、、、、、、、、、、、、、、、、、、、、、、、、、、

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

int temp;

int n,f[2200];

int maxx=0;

struct mmm

{

    int one,another;

    int buckets;

}a[1200];

bool cmp(mmm x,mmm y)

{

    return (x.another<y.another);

}

void init()

{

    memset(f,0,sizeof(f));

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

    {

        scanf("%d%d",&a[i].one,&a[i].another);

        a[i].buckets=a[i].another-a[i].one+1;

    }

    memset(f,0,sizeof(f));

}

void work()

{

    sort(a+1,a+1+n,cmp);

    for(int i=1;i<=n;i++)//以最后一个区间的end作为i max

    {

        f[i]=a[i].buckets;

        maxx=max(f[i],maxx);

    }

    for(int i=2;i<=n;i++)

    {

        temp=0;

        for(int j=1;j<i;j++)

            if(a[i].one>a[j].another&&temp<f[j])

                temp=f[j];

        f[i]+=temp;

        maxx=max(maxx,f[i]);

    }

    printf("%d\n",maxx);

}

int main()

{

    init();

    work();

    return 0;

}