题目链接:here~~
以后做题要认真读题,搞清楚每个条件!
这道题,是要求单调的最长子序列,单调可能递增,也可能递减,所以要把两个的都求出来比较一下!(这题和拦截导弹的那个题有点类似,可以参考一下!拦截导弹)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
struct con
{
int x, y;
bool operator < (const con &r) const
{
return r.x>x;
}
}a[1000];
int b[1000], c[1000];
int main()
{
int T, n, x, i, j, max, max2, res;
// freopen("in.txt", "r", stdin);
scanf("%d", &T);
while(T--)
{
memset(a, 0, sizeof(a));
memset(b, 1, sizeof(b));
memset(c, 1, sizeof(c));
scanf("%d", &n);
for (i = 0; i<n; i++)
scanf("%d%d", &a[i].x, &a[i].y);
sort(a, a+n);
res = 0;
for (i = 0; i<n; i++)
{
max = 1;max2 = 1;
for (j = 0; j<i; j++)
{
if (a[i].y>a[j].y&&max<=b[j])
max = b[j]+1;
if (a[i].y<a[j].y&&max2<=c[j])
max2 = c[j]+1;
}
b[i]=max;
c[i] = max2;
if (res<max)
res=max;
if(res<max2)
res=max2;
}
printf("%d\n", res);
}
return 0;
}