hdu6119 小小粉丝度度熊【尺取】

82 篇文章 0 订阅
48 篇文章 0 订阅

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6119
题意:中文题
解析:首先需要能合并的区间都合并了(按L排序后扫一遍),因为题目有明确说明,接着把每个区间之间的差值处理出来,然后尺取做一遍,维护一下答案即可,要注意,剩下的补签卡,可以直接签在已经维护好的那个区间的后面

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
typedef long long ll;
struct node
{
    ll l,r;
    bool operator < (const node &b)const
    {
        if(l==b.l)
            return r<b.r;
        return l<b.l;
    }
}tmp[maxn],a[maxn];
ll d[maxn];
int main(void)
{
    ll n,m;
    while(~scanf("%I64d %I64d",&n,&m))
    {
        for(int i=0;i<n;i++)
            scanf("%I64d %I64d",&tmp[i].l,&tmp[i].r);
        sort(tmp,tmp+n);
        int cnt = 1;
        a[cnt] = tmp[0];
        for(int i=1;i<n;i++)
        {
            if(tmp[i].l<=a[cnt].r+1)
                a[cnt].r = max(tmp[i].r,a[cnt].r);
            else
            {
                cnt++;
                a[cnt] = tmp[i];
            }
        }
        for(int i=1;i<cnt;i++)
            d[i] = a[i+1].l-a[i].r-1;
        int t1 = 0,l = 1;
        ll sum = a[1].r-a[1].l+1+m;
        for(int i=1;i<cnt;i++)
        {
            t1 += d[i];
            while(t1>m)
            {
                t1 -= d[l];
                l++;
            }
            sum = max(sum,a[i+1].r-a[l].l+1+m-t1);
        }
        printf("%I64d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值