作业题 南工201

题目链接: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值