解题报告 『LCIS(线性动规)』

原题地址

万万没想到用快读T了一个点,取消同步后的cin居然过了……

代码实现如下:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (register int i = a; i <= b; i++)

const int maxn = 3e3 + 5;

int n, ans;
int a[maxn], b[maxn], dp[maxn][maxn];

int MAX(int a, int b) {return a > b ? a : b;}

void write(int x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    rep(i, 1, n) cin >> a[i];
    rep(i, 1, n) cin >> b[i];
    rep(i, 1, n) {
        int val = 0;
        rep(j, 1, n) {
            dp[i][j] = (a[i] == b[j]) ? MAX(dp[i][j], val + 1) : dp[i - 1][j];
            if (b[j] < a[i]) val = MAX(val, dp[i - 1][j]);//本题数据也是够弱的,一开始写成了val = MAX(val, dp[i][j])也能A. 
        }
    }
    rep(j, 1, n) ans = MAX(ans, dp[n][j]);
    write(ans);
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/Kirisame-Marisa/p/10806631.html
今日推荐