首页 > 其他 > 详细

CodeVS 3027 线段覆盖2

时间:2017-09-06 15:07:52      阅读:269      评论:0      收藏:0      [点我收藏+]

题目大意:

http://codevs.cn/problem/3027/

 

源码:

技术分享
#include <iostream>

using namespace std;

struct {
    int x,y,val;
}tmp[1050];

int dp[1050] = {0};

int main()
{
    int n;
    cin >> n ;
    for(int i = 1; i <= n; i++)
    {
        cin >> tmp[i].x >> tmp[i].y >> tmp[i].val;
    }

    tmp[0].x = tmp[0].y = tmp[0].val = 0;

    //排序方式很low啊,哈哈哈
    for(int i = 1 ; i <= n; i++ )
    {
        for(int j = i+1; j <= n; j++)
        {
            if(tmp[i].y > tmp[j].y)
            {
                int tmpx = tmp[i].x;
                int tmpy = tmp[i].y;
                int tmp_val = tmp[i].val;

                tmp[i].x = tmp[j].x;
                tmp[i].y = tmp[j].y;
                tmp[i].val = tmp[j].val;
                tmp[j].x = tmpx;
                tmp[j].y = tmpy;
                tmp[j].val = tmp_val;
            }
        }
    }

    dp[0] = 0;
    
    for(int i = 1; i <= n; i++)
    {
        for(int j = 0; j < i; j++)
        {
            if(tmp[i].x >= tmp[j].y)
            {
                dp[i] = max(dp[i-1], dp[j]+tmp[i].val);

            }
        }
    }


    cout << dp[n] << endl;
    
    return 0;
}
View Code

 

这个排序方式很差啊,参考一下别人的:

 

结构体:

struct {
    int x,y,val;
}tmp[1050];

排序:

#include<algorithm>


sort(tmp, tmp + n, comp);

函数:

int comp(tmp a, tmp b) {
    return a.y < b.y;
}

 

CodeVS 3027 线段覆盖2

原文:http://www.cnblogs.com/zyqBlog/p/7484804.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!