饥饿的牛(hunger)

饥饿的牛(hunger)

题目描述

牛在饲料槽前排好了队。饲料槽依次用1到n(1≤n≤2000)编号。每天晚上,一头幸运的牛根据约翰的规则,吃其中一些槽里的饲料。约翰提供B个区间的清单。一个区间是一对整数start-end,1≤start

输入

第1行,整数B(1≤B≤I000);
第2到B+1行,每行两个整数,表示一个区间,较小的端点在前面。

输出

仅一个整数,表示最多能吃到多少个槽里的食物。

样例输入

3
1 3
7 8
3 4

样例输出

5
分析:类似01背包的动态规划,01背包限制容量,这里限制区间不能有重叠;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e4+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,dp[maxn],ma;
pii a[maxn];
int main()
{
    int i,j,k,t;
    scanf("%d",&n);
    rep(i,0,n-1)scanf("%d%d",&a[i].se,&a[i].fi),ma=max(ma,a[i].fi);
    sort(a,a+n);
    rep(i,0,n-1)
    {
        for(j=ma;j>=a[i].fi;j--)
            dp[j]=max(dp[j],dp[a[i].se-1]+a[i].fi-a[i].se+1);
    }
    printf("%d
",dp[ma]);
    //system ("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/dyzll/p/5681646.html