poj 3161 Milking Time

题意:有m段区间选取不相交的多个区间,是的加和最大

思路:真是蠢的要死,变成区间就不会了,其实和lis一样(lis真是最好的算法 )

代码:

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define zero(a) fabs(a)<eps
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )
#define lowbit(x) (x&(-x))
typedef long long ll;
const double pi=acos(-1.0);
const double eps=1e-8;
const int inf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
const int maxn = 1007;
using namespace std;

int n,m,k;
struct node{int l,r,val;}a[maxn];
int dp[maxn];
bool cmp(node x,node y)
{
    return x.l<y.l;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k)){
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].val);
            a[i].r+=k;
        }
        sort(a,a+m,cmp);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<m;i++){
            dp[i]=a[i].val;
            for(int j=0;j<i;j++){
                if(a[i].l>=a[j].r){
                    dp[i]=max(dp[i],dp[j]+a[i].val);
                }
            }
        }
        int maxe=0;
        for(int i=0;i<m;i++){
            maxe=max(maxe,dp[i]);
        }
        printf("%d\n",maxe);
    }
    return 0;
}

 

posted @ 2018-02-01 21:49  啦啦啦天啦噜  阅读(138)  评论(0编辑  收藏  举报