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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6119

题意:中文题面。

解法:先处理可能交叉的区间,然后容易发现满足双指针的特性。

//HDU 6119
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9+7;
const int maxn = 1e5+10;
pair<LL,LL>p[maxn];
LL n,m,sum[maxn];

int main()
{
    while(~scanf("%lld%lld",&n,&m))
    {
        for(int i=1; i<=n; i++) scanf("%lld%lld",&p[i].first,&p[i].second);
        sort(p+1,p+n+1);
        int cnt=0;
        for(int i=1; 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++]=make_pair(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;
}

 

posted @ 2017-08-16 16:13  zxycoder  阅读(168)  评论(0编辑  收藏  举报