码迷,mamicode.com
首页 > 其他好文 > 详细

CodeVS 3027 线段覆盖2

时间:2017-09-06 15:40:36      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:stream   namespace   std   code   algo   main   排序   ref   int   

题目大意:

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

标签:stream   namespace   std   code   algo   main   排序   ref   int   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!