码迷,mamicode.com
首页 > 编程语言 > 详细

hdu 1160 排序 + 最长上升子序列

时间:2015-05-24 10:12:51      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:序列   结构体   

题意:

  • 输出体重上升而速度下降的最长子序列

题意:

  • 先按照结构体升序排序体重,之后用dp对速度求最长下降子序列即可。

代码:

#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long int LL;
const int M = 1009,INF = 0x3fffffff;
struct place {int x, y, z;} a[M];
pair<int, int> dp[M];
bool cmp(struct place a, struct place b) {
    if(a.x == b.x) return a.y > b.y;
    if(a.x < b.x) return true;
    return false;
}

int main(void) {
    //problem:hdu 1160  address:http://acm.hdu.edu.cn/showproblem.php?pid=1160
    int n = 0;
    while(cin >> a[n].x >> a[n].y) {
        a[n].z = n + 1;
        n++;
    }
    sort(a, a + n, cmp);
    //for(int i = 0; i < n; i++) cout << a[i].x << " " << a[i].y << " " << a[i].z << endl;
    for(int i = 0; i < n; i++) dp[i].first = 1;
    int ans = 0, key = 0;
    for(int i = 0; i < n; i++) {
        for(int j = i - 1; j >= 0; j--) {
            if(a[i].x != a[j].x && a[i].y < a[j].y && dp[i].first < dp[j].first + 1) {
                dp[i].second = j;
                dp[i].first = dp[j].first + 1;
            }
        }
        //for(int ii = 0; ii < n; ii++) cout << dp[ii].first << " " << dp[ii].second << "   ";
        //cout << endl;
        if(ans <= dp[i].first) {key = i; ans = dp[i].first;}
    }
    cout << ans << endl;
    stack<int> s;
    while(ans--) {
        s.push(a[key].z);
        key = dp[key].second;
    }
    while(!s.empty()) {
        cout << s.top() << endl;
        s.pop();
    }
    return 0;
}

hdu 1160 排序 + 最长上升子序列

标签:序列   结构体   

原文地址:http://blog.csdn.net/jibancanyang/article/details/45949591

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