CodeVS 1214 线段覆盖(DP)

题目大意:

http://codevs.cn/problem/1214/

代码:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

struct type{
    int x,y;
};

int dp[110];
type arr[100];

bool cmp(type a,type b)
{
    if(a.y == b.y)
        return a.x < b.x;
    return a.y < b.y;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> arr[i].x >> arr[i].y;
        if(arr[i].x > arr[i].y)
            swap(arr[i].x,arr[i].y);
    }

    sort(arr+1,arr+1+n,cmp);
    memset(dp,0,sizeof(dp));


    int ans = 1;
    dp[1] = 1;
    for(int i = 2; i <= n; i++)
    {
        for(int j = i - 1; j >= 1; j--)
        {
            if(arr[i].x >= arr[j].y)
                dp[i] = max(dp[i],dp[j]+1);
        }

        ans = max(ans,dp[i]);
    }

    cout << ans << endl;

    return 0;
}
View Code

 

posted @ 2017-09-06 22:05  prog123  阅读(196)  评论(0编辑  收藏  举报