解题思路:
最长不降子序列和最长上升子序列
#include <iostream> #include <stdio.h> #include <map> #include <vector> #include <algorithm> #include <cstring> using namespace std; typedef long long ll; const int MAXN = 101; int dp[MAXN]; int ans[MAXN]; int n = 1; int maxx, num; void f1() { for (int i = 1; i < n; i++) { dp[i] = 1; for (int j = 1; j < i; j++) { if (ans[j] > ans[i] && dp[i] < dp[j] + 1) { dp[i] = dp[j] + 1; } } if (maxx < dp[i]) maxx = dp[i]; } } void f2() { for (int i = 1; i <= n; i++) { dp[i] = 1; for (int j = 1; j < i; j++) { if (ans[j] < ans[i] && dp[i] < dp[j] + 1) { dp[i] = dp[j] + 1; } } if (num < dp[i]) num = dp[i]; } } int main() { while (cin >> ans[n]) n++; f1(); memset(dp, 0, sizeof(dp)); f2(); cout << maxx << endl << num << endl; return 0; }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题5.7 (C++代码)浏览:879 |
P1002 (C语言代码)浏览:1019 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:485 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:467 |
【魔板】 (C++代码)(时间超限,希望会的帮我改正一下)浏览:804 |
Cylinder (C语言描述,蓝桥杯)浏览:1279 |
A+B for Input-Output Practice (V) (C语言代码)浏览:497 |
1128题解(返回值为数组的情况)浏览:571 |
DNA (C语言代码)浏览:798 |
C二级辅导-温度转换 (C语言代码)浏览:802 |