zoukankan      html  css  js  c++  java
  • 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
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    lambda Join /Group by/ Contains
    PDF ITextSharp
    sql server基本流程语句
    easyui DateTimeBox OK
    Axuer 网页
    蓝桥杯- 煤球数目-java
    蓝桥杯-有奖猜谜-java
    蓝桥杯-打印大X-java
    蓝桥杯- 移动距离-java
    蓝桥杯-加法变乘法-java
  • 原文地址:https://www.cnblogs.com/spfa/p/7373908.html
Copyright © 2011-2022 走看看