JZOJ5350. 【NOIP2017提高A组模拟9.7】陶陶摘苹果

87 篇文章 0 订阅

这里写图片描述

题解

这里的m比较小,自然想到dp
fi,j 表示在前i个区间里面,选了j个的最大值,
转移比较简单,注意区间相交的地方。
当两个区间有相交的时候,要将里面重复的地方去掉。

code

#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#define ll long long
#define N 203
#define db double
#define P putchar
#define G getchar
#define mo 1000000007
using namespace std;
char ch;
void read(int &n)
{
    n=0;
    ch=G();
    while((ch<'0' || ch>'9') && ch!='-')ch=G();
    ll w=1;
    if(ch=='-')w=-1,ch=G();
    while('0'<=ch && ch<='9')n=n*10+ch-'0',ch=G();
    n*=w;
}

int max(int a,int b)
{
    return a>b?a:b;
}

void write(int x)
{
     if(x>9) write(x/10);
     P(x%10+'0');
}

struct node
{
    int l,r;
}a[N];

bool cmp(node a,node b)
{
    return (a.l<b.l) || (a.l==b.l && a.r>b.r);
}

int f[N][N],n,m,h,k,x,sum,ans,mx,s[N*5000],v[N];

int main()
{
    freopen("apple.in","r",stdin);
    freopen("apple.out","w",stdout);
    read(n);read(m);read(h);read(k);mx=sum=ans=0;
    for(int i=1;i<=n;i++)
    {
        read(x);
        mx=max(mx,x);
        if(x>h)s[x-h]++;
        if(x==h)sum++;
    }

    for(int i=1;i<=m;i++)
    {
        read(a[i].l),read(a[i].r);
        if(a[i].l==0)a[i].l=1;
        mx=max(mx,a[i].r);
    }

    for(int i=1;i<=mx;i++)
        s[i]+=s[i-1];

    x=0;
    for(int i=1;i<=m;i++)   
        for(int j=1;j<=m;j++)
            if(i!=j && a[i].l!=2147483647 && a[i].l<=a[j].l && a[j].r<=a[i].r)x++,a[j].l=a[j].r=2147483647;

    sort(a+1,a+1+m,cmp);
    m-=x;
    for(int i=1;i<=m;i++)
        v[i]=s[a[i].r]-s[a[i].l-1];

    for(int i=1;i<=m;i++)
        for(int j=1;j<=min(i,k);j++)
        {
            for(int t=0;t<=i-1;t++)
            {
                if(a[t].r>=a[i].l)f[i][j]=max(f[i][j],f[t][j-1]+v[i]-s[a[t].r]+s[a[i].l-1]);
                    else f[i][j]=max(f[i][j],f[t][j-1]+v[i]);
            }
            ans=max(ans,f[i][j]);
        }

    write(ans);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值