Luogu2439 [SDOI2005]阶梯教室设备利用 (动态规划)

同上一题,区间改左闭右开就双倍经验了。貌似可以跑最长路。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 10007;

struct Field{
    int l, r, w;
    bool operator < (const Field &com) const{
        if(r != com.r) return r < com.r;
        return l < com.l;
    }
}a[N];

int f[N];

inline int Find(int x){
    int l = 1, r = x;
    int ans = -1;
    while(l <= r){
        int mid = (l + r) >> 1;
        if(a[mid].r < a[x].l){
            l = mid + 1;
            ans = mid;
        }
        else{
            r = mid - 1;
        }
    }
    return ans;
}
int main(){
    //FileOpen();
    int n;
    io >> n;
    R(i,1,n){
        io >> a[i].l >> a[i].r;
        --a[i].r;
        a[i].w = a[i].r - a[i].l + 1;
    }

    sort(a + 1, a + n + 1);
    
    f[1] = a[1].w;
    R(i,2,n){
        int j = Find(i);
        if(j != -1)
            f[i] = Max(f[i - 1], f[j] + a[i].w);
        else
            f[i] = Max(f[i - 1], a[i].w);
    }
    
    printf("%d\n",f[n]);
    
    return 0;
}

1570282-20190721154208561-750863380.png

转载于:https://www.cnblogs.com/bingoyes/p/11221389.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值