小小粉丝度度熊

传送门
太坑了。。。。
一个点并不是一个N,M。被坑了好半天
首先要先合并区间,将输入的区间以L为第一关键字,R为第二关键字排序
首先把第一个区间加入进去,然后枚举区间,如果枚举区间的L<=当前区间R+1,就将枚举的区间扔进此区间去,R取MAX
如果L>当前区间R+1证明区间断开,直接把这个枚举的区间作为当前的合并区间(因为L升序排列)
然后就是求最优方案了,首先我们连接的区间肯定是连续的。。。。
但是怎样枚举区间呢
先出来相邻两个区间的差值(注意-1)
以I为右指针,L为左指针,每次把len[i]加入到一个变量NOW中去,表示当前使用的补签卡的数量
当NOW>M时,让l++,同时把len[l]从NOW中减出去,就完成了对每种符合要求的情况的枚举
每次枚举出的长度是啥呢
ra[i+1].r-ra[l].l+1+m-now(+m是要考虑把剩下的补签卡都用掉的情况)
但是如果只有一个大区间咋办,我们直接在这个大区间后面补补签卡就行了

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#define ll long long 
using namespace std;
struct node{
    ll l,r;
};
node a[200000],ra[200000];
ll len[200000];
ll n,m;
bool comp(const node&a,const node&b)
{
    if(a.l==b.l) return a.r<b.r;
    return a.l<b.l;
}
void work()
{
    for(ll i=1;i<=n;i++)
     scanf("%lld%lld",&a[i].l,&a[i].r);
    sort(a+1,a+n+1,comp);
    ra[1]=a[1];
    ll cnt=1;
    for(ll i=2;i<=n;i++)
     if(ra[cnt].r+1>=a[i].l)
      ra[cnt].r=max(ra[cnt].r,a[i].r);
     else 
      ra[++cnt]=a[i];
    for(ll i=1;i<cnt;i++)
     len[i]=ra[i+1].l-ra[i].r-1;
    ll l=1,now=0;
    ll ans=0;
    for(ll i=1;i<cnt;i++)
    {
        now+=len[i];
        while(m<now)
         now-=len[l++];
        ans=max(ans,ra[i+1].r-ra[l].l+1+m-now);
    }
    ans=max(ans,ra[1].r-ra[1].l+1+m);
    printf("%lld\n",ans);
}
int main()
{
    while(scanf("%lld%lld",&n,&m)!=EOF)
    work();
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值