题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1087
题目大意:
求递增子序列最大和
思路:
直接dp就可以求解,dp[i]表示以第i位结尾的递增子序列最大和,初始化dp[i] = a[i],转移方程dp[i] = max(dp[i], a[i] + dp[j])如果j < i && a[j] < a[i]
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<string> 6 using namespace std; 7 typedef long long ll; 8 int cases; 9 const int maxn = 1e5 + 100; 10 typedef long long ll; 11 int n; 12 int a[maxn], dp[maxn];//dp[i]表示以第i位结尾的最大上升序列和 13 //dp[i] = max(dp[i], a[i] + dp[j])如果j < i && a[j] < a[i] 14 int main() 15 { 16 while(cin >> n && n) 17 { 18 for(int i = 0; i < n; i++) 19 { 20 cin >> a[i]; 21 dp[i] = a[i]; 22 } 23 for(int i = 0; i < n; i++) 24 { 25 for(int j = 0; j < i; j++) 26 { 27 if(a[j] < a[i])dp[i] = max(dp[i], a[i] + dp[j]); 28 } 29 } 30 int ans = dp[0]; 31 for(int i = 1; i < n; i++)ans = max(ans, dp[i]); 32 cout<<ans<<endl; 33 } 34 return 0; 35 }