HDU 1087 && POJ 2533(DP,最长上升子序列).

~~~~

两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度。

貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛。。

~~~~

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1087

http://poj.org/problem?id=2533

~~~~

HDU1087:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x7fffffff
#define N 1111
using namespace std;

int f[N],a[N];
int main()
{
    int n;
    while(~scanf("%d",&n),n)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            f[i]=a[i];
        }
        int ans=f[1];
        for(int i=2;i<=n;i++)
        {
            for(int j=1;j<i;j++)
            {
                if(a[i]>a[j])
                    f[i]=max(a[i]+f[j],f[i]);
            }
            ans=max(ans,f[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

~~~~

POJ 2533:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x7fffffff
#define N 1111
using namespace std;

int f[N],a[N];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            f[i]=1; //~~
        }
        int ans=f[1];
        for(int i=2;i<=n;i++)
        {
            for(int j=1;j<i;j++)
            {
                if(a[i]>a[j])
                    f[i]=max(1+f[j],f[i]);  //~~
            }
            ans=max(ans,f[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

HDU 1087 && POJ 2533(DP,最长上升子序列).,布布扣,bubuko.com

时间: 2024-10-05 05:25:29

HDU 1087 && POJ 2533(DP,最长上升子序列).的相关文章

N - Longest Ordered Subsequence POJ 2533 (最长上升子序列 )

N - Longest Ordered Subsequence Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numer

Longest Ordered Subsequence POJ - 2533 dp 最长上升/不下降 子序列

#include<iostream> using namespace std ; const int N=1010; int f[N]; int a[N]; int n; int main() { cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; for(int i=1; i<=n; i++) { f[i]=1; for(int j=1; j<=i; j++) { if(a[j]<a[i]) { f[i]=ma

POJ 2250 Compromise (DP,最长公共子序列)

Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6440 Accepted: 2882 Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfille

hdu 1025 dp 最长上升子序列

1 //Accepted 4372 KB 140 ms 2 //dp 最长上升子序列 nlogn 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 500005; 8 int dp[imax_n]; 9 int d[imax_n]; 10 int a[imax_n]; 11 int n; 12 int len

HDU 1160 FatMouse&#39;s Speed (最长上升子序列+记录路径)

题目链接:HDU 1160 FatMouse's Speed 题意:求体重越重,反而速度越慢的例子,并输出对应的编号. 对speed进行从大到小排序,再求weight的最长上升序列,并输出路径. AC代码: #include<stdio.h> #include<algorithm> #include<stack> using namespace std; struct Node { int weight; int speed; int id; }; struct Nod

HDU 1160 FatMouse&#39;s Speed (最长上升子序列)

题目链接 题意:n个老鼠有各自的重量和速度,要求输出最长的重量依次严格递增,速度依次严格递减的序列,n最多1000,重量速度1-10000. 题解:按照重量递增排序,找出最长的速度下降子序列,记录序列每个位置的左边的位置,找到最大值终点再递归输出即可.(好久没做题了,花了很多时间才AC.) #include <bits/stdc++.h> using namespace std; struct sa { int weight,speed,pos,l; } data[1005]; bool cm

poj1836——dp,最长上升子序列(lis)

poj1836——dp,最长上升子序列(lis) Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13767   Accepted: 4450 Description In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight

codeforces340D - Bubble Sort Graph dp + 最长上升子序列

题意:给你长为n的序列 ,每个节点都和在它前面且值比他大的点产生一条边,问你一个最大 两两点没有边的集合的 集合元素有多少 解题思路:想了半天才发现是最长上升子序列.. 解题代码: 1 // File Name: 340d.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月03日 星期日 12时31分24秒 4 5 #include<vector> 6 #include<list> 7 #include<map>

UVA 10131 Is Bigger Smarter?(DP最长上升子序列)

Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible in