hdu 5087(LIS变形)
生活随笔
收集整理的這篇文章主要介紹了
hdu 5087(LIS变形)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5087
解題思路:這道題其實仔細想想很簡單,次長LIS只有兩種可能,一種就是等于LIS-1,一種就是LIS,什么時候就是等于LIS呢?肯定LIS的個數不唯一。轉化到這里,這道題就比較簡單了。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 1005; int step[maxn],dp[maxn], num[maxn]; int main () {int T;scanf("%d", &T);while(T--){int n;scanf("%d", &n);memset(dp, 0, sizeof(dp));memset(step, 0, sizeof(step));dp[0] = 1;for(int i = 1; i <= n; i++)scanf("%d", &num[i]);int Max = -1;for(int i = 1; i <= n; i++){for(int j = 0; j < i; j++){if(num[i] > num[j]){if(step[j]+1 > step[i]){step[i] = step[j] + 1;dp[i] = dp[j];}else if(step[j] + 1 == step[i])dp[i] += dp[j];}}Max = max(Max, step[i]);}int flag = 0;int ok = 1;for(int i = 1; i <= n; i++){if(step[i] == Max){if(dp[i] > 1) ok = 0;else{if(flag) ok = 0;flag = 1;}}}printf("%d\n", Max - ok );}return 0; }總結
以上是生活随笔為你收集整理的hdu 5087(LIS变形)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu 5067(状态压缩dp)
- 下一篇: 前端有关vue的面试题