CODEVS_3027 线段覆盖2

来源:互联网 发布:淘宝宝贝排名查询工具 编辑:程序博客网 时间:2024/05/21 05:42

题目地址

题目描述 Description
数轴上有n条线段,线段的两端都是整数坐标,坐标范围在0~1000000,每条线段有一个价值,请从n条线段中挑出若干条线段,使得这些线段两两不覆盖(端点可以重合)且线段价值之和最大。

n<=1000

输入描述 Input Description
第一行一个整数n,表示有多少条线段。

接下来n行每行三个整数, ai bi ci,分别代表第i条线段的左端点ai,右端点bi(保证左端点<右端点)和价值ci。

输出描述 Output Description
输出能够获得的最大价值
样例输入 Sample Input
3

1 2 1

2 3 2

1 3 4

样例输出 Sample Output
4

数据范围及提示 Data Size & Hint
数据范围

对于40%的数据,n≤10;

对于100%的数据,n≤1000;

0<=ai,bi<=1000000

0<=ci<=1000000

#include<stdio.h>#include <algorithm>using namespace std;const int Max_N(1000);int N;int dp[Max_N];struct point{    int x, y;    int s;}arr[Max_N];int cmp(point a, point b){    if(a.y == b.y)        return a.x > b.x;    return a.y < b.y;}void solve(){    int ans(0);    sort(arr, arr + N, cmp);    for(int i = 0; i < N; ++i){        dp[i] = arr[i].s;        for(int j = 0; j < i; ++j){            if(arr[i].x >= arr[j].y)                dp[i] = max(dp[i], dp[j] + arr[i].s);        }        ans = max(dp[i], ans);    }    printf("%d\n", ans);}int main(){    scanf("%d", &N);    for(int i(0); i < N; ++i){        scanf("%d %d %d", &arr[i].x, &arr[i].y, &arr[i].s);    }    solve();    return 0;}
0 0