0
点赞
收藏
分享

微信扫一扫

nyist 201 作业题(最长上升子序列和最长下降子序列)


题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=201

思路:注意有可能单调递增和单调递减

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
struct node
{
int x;
int y;
}a[1000];
bool cmp(node a,node b)
{
return a.x < b.x;
}
int dp[1000];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+n+1,cmp);
int max2 = 0;
for(int i=1; i<=n ;i++)
{
int max1 = 0;
for(int j=1; j<i; j++)
{
if(a[i].y > a[j].y && dp[j] > max1)
max1 = dp[j];
}
dp[i] = max1 + 1;
if(dp[i] > max2)
max2 = dp[i];
}
for(int i=1; i<=n; i++)
{
int max1 = 0;
for(int j=1; j<i; j++)
{
if(a[i].y < a[j].y && dp[j] > max1)
max1 = dp[j];
}
dp[i] = max1 + 1;
if(dp[i] > max2)
max2 = dp[i];
}
printf("%d\n",max2);
}
return 0;
}



举报

相关推荐

0 条评论