【Codevs3027】线段覆盖2

problem

solution

codes

//f[i]:到第i条线段为止能获得的最大价值
//f[i]=max{s[i].c,f[j]+s[i].c|j
#include
#include
using namespace std;
const int maxn = 1010;
struct seg{ int a, b, c; }s[maxn];
bool cmp(seg a, seg b){ return a.aint n, f[maxn], ans;
int main(){
    cin>>n;
    for(int i = 1; i <= n; i++)
        cin>>s[i].a>>s[i].b>>s[i].c;
    sort(s+1,s+n+1,cmp);
    for(int i = 1; i <= n; i++){
        f[i] = s[i].c;
        for(int j = 1; j < i; j++)
            if(s[j].b<=s[i].a)f[i] = max(f[i], f[j]+s[i].c);
        ans = max(ans, f[i]);
    }
    cout<"\n";
    return 0;
}

你可能感兴趣的:(算法)