- 瓜子笔试题
- 求数组高度
- 保存的是j - i
- 若求最长不连续子序列个数,dp[i] = max(dp[j] + (i - j), dp[i]); 改成 dp[i] = max(dp[j] + 1, dp[i]);
-
#include<iostream> #include<vector> #include<algorithm> #include<cmath> using namespace std; long long getresult(vector<long long> nums, long long n) { vector<long long> dp(n + 1); for (long long i = 0; i < n; i++) { dp[i] = 1; for (long long j = 0; j < i; j++) { if (nums[j] < nums[i]) { dp[i] = max(dp[j] + (i - j), dp[i]); } } } long long maxx = 0; for (long long i = 0; i < n; i++) { if (maxx < dp[i]) { maxx = dp[i]; } } return maxx; } bool cmp(long long a, long long b) { return (a > b); } int main() { long long n; cin >> n; vector<long long> nums(n); vector<long long> numy(n); long long max = 0; long long start = 0; for (long long i = 0; i < n; i++) { long long m; cin >> m; nums[i] = m; numy[i] = m; } if (n == 0 || n == 1) { cout << 0; return 0; } sort(numy.begin(), numy.end(), cmp); long long si = 0, yi = 0; while (nums[si] == numy[yi]) { si++; yi++; if (si == (n - 1)) { cout << 0; return 0; } } cout << getresult(nums, n); return 0; }
2020 瓜子笔试题——最长不连续子序列
最新推荐文章于 2023-07-31 21:55:08 发布