HDU 6119 小小粉丝度度熊 双指针

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6119

题意:给你n个人区间 可能重合包含 给你m次机会每次可以填补一个点 求最大连续长度

思路:数据量较大 但是把区间处理后发现 符合双指针的连续性

注意:处理连续重合区间

code:

 

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL>P;
const int mod  = 1e9 + 7;
const int maxn = 2e5 + 100;
P p[maxn];
LL sum[maxn];
LL n, m;
int main()
{
    while(scanf("%lld%lld", &n, &m) != EOF)
    {
        for(int i = 0; i < n; i++)
        {
            LL l, r;
            scanf("%lld%lld", &l, &r), p[i] = P(l, r);
        }
        sort(p , p + n);
        int cnt = 0;
        for(int i = 0; i < n; i++)
        {
            LL l = i, r = i + 1, last = p[i].second;
            while(r < n && p[r].first <= last)
            {
                last = max(last, p[r].second),r++;
            }
            r--;
            p[cnt++] = P(p[l].first, last);
            i = r;
        }
        sum[0]=0;
        for(int i=1;i<cnt;i++) sum[i]=sum[i-1]+p[i].first-p[i-1].second-1;
        LL ans=m;int r=0;
        for(int l=0;r<cnt && l < cnt;l++)
        {
            while(sum[r]- sum[l] <= m && r < cnt) r++;
            LL x=m-(sum[r-1] - sum[l]);
            ans=max(ans,p[r-1].second-p[l].first+1+x);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值