1 5 1 4 2 5 -12 4 -12 1 2 4
2
求最长公共上升子序列。
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<stack> using namespace std; const int maxn = 505; int a[maxn], b[maxn], n, m, T, f[maxn][maxn], Max, ans; int main() { scanf("%d", &T); while (T--) { memset(f, 0, sizeof(f)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (a[i] == b[j]) { Max = 0; for (int k = 1; k < j; k++) if (b[k] < b[j]) Max = max(Max, f[i - 1][k]); f[i][j] = Max + 1; ans = max(ans, Max + 1); } else f[i][j] = f[i - 1][j]; printf("%d\n", ans); if (T) printf("\n"); } return 0; }#include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<stack> using namespace std; const int maxn = 505; int a[maxn], b[maxn], n, m, T, f[maxn][maxn], Max, ans; int main() { scanf("%d", &T); while (T--) { memset(f, 0, sizeof(f)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); ans = 0; for (int i = 1; i <= n; i++) { Max = 0; for (int j = 1; j <= m; j++) if (a[i] == b[j]) { f[i][j] = Max + 1; ans = max(ans, Max + 1); } else { if (a[i] > b[j]) Max = max(Max, f[i - 1][j]); f[i][j] = f[i - 1][j]; } } printf("%d\n", ans); if (T) printf("\n"); } return 0; }#include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<stack> using namespace std; const int maxn = 505; int a[maxn], b[maxn], n, m, T, f[maxn], Max, ans; int main() { scanf("%d", &T); while (T--) { memset(f, 0, sizeof(f)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); ans = 0; for (int i = 1; i <= n; i++) { Max = 0; for (int j = 1; j <= m; j++) if (a[i] == b[j]) { f[j] = Max + 1; ans = max(ans, f[j]); } else if (a[i] > b[j]) Max = max(Max, f[j]); } printf("%d\n", ans); if (T) printf("\n"); } return 0; }