百(垃)度(圾)之星初赛B hdu6119

小小粉丝嘟嘟熊

题意:中文题

思路:大概就是尺取法,先按l从小到大再按r从大到小对区间排序,L,R表示当前计算的可行的区间,依次取每个区间,然后更新L,R,每次计算答案取最大值。。。数据有问题,wa了一天,不愧为百(垃)度(圾)之星

AC代码:

#include "iostream"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#pragma comment(linker, "/STACK:102400000,102400000")
#define ll long long
#define endl ("\n")
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a,x) memset(a,x,sizeof(a))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define ft first
#define sd second
#define lrt (rt<<1)
#define rrt (rt<<1|1)
using namespace std;
const long long INF = 1e18+1LL;
const int inf = 1e9+1e8;
const int N=1e5+100;
const ll mod=1e9+7;

struct Node{
    ll l, r;
    bool friend operator< (Node a, Node b){
        if(a.l==b.l) return a.r>b.r;
        return a.l<b.l;
    }
}a[N],x;
queue<Node> Q;
int main(){
    ll n,m;
    while(scanf("%I64d %I64d",&n, &m)!=EOF){
        for(int i=1; i<=n; ++i){
            scanf("%I64d %I64d",&a[i].l, &a[i].r);
        }
        sort(a+1,a+1+n);
        while(!Q.empty()) Q.pop();
        ll L=0, R=-1,ans=0;
        for(int i=1; i<=n; ++i){
            if(a[i].l-1>R){
                if(a[i].l-R-1<=m){
                    m-=a[i].l-R-1;
                    x.l=R+1, x.r=a[i].l-1;
                    Q.push(x);
                    R=a[i].r;
                    ans=max(ans,m+R-L+1);// cout<<m+R-L+1<<" 1"<<endl;
                }
                else{
                    while(!Q.empty()){
                        x=Q.front(); Q.pop();
                        L=x.r+1;
                        ll c=x.r-x.l+1;
                        m+=c;
                        if(a[i].l-R-1<=m) break;
                    }
                    if(a[i].l-R-1<=m){
                        m-=a[i].l-R-1;
                        x.l=R+1, x.r=a[i].l-1;
                        Q.push(x);
                        R=a[i].r;
                        ans=max(ans,m+R-L+1); //cout<<m+R-L+1<<" 2"<<endl;
                    }
                    else{
                        x.l=a[i].l-m, x.r=a[i].l-1;
                        Q.push(x); m=0;
                        L=x.l, R=a[i].r;
                        ans=max(ans, R-L+1); //cout<<m+R-L+1<<" 3"<<endl;
                    }
                }
            }
            else{   //cout<<m+R-L+1<<" 4"<<endl;
                if(a[i].r>R){
                    R=a[i].r;
                    ans=max(ans,m+R-L+1);
                }
                else ans=max(ans,m+R-L+1);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

posted on 2017-08-13 19:26  lazzzy  阅读(159)  评论(0编辑  收藏  举报

导航