其他分享
首页 > 其他分享> > 洛谷 P3009 [USACO11JAN]利润Profits

洛谷 P3009 [USACO11JAN]利润Profits

作者:互联网

嗯...

 

题目链接:https://www.luogu.org/problemnew/show/P3009

 

这是DP的另一个功能,求最大子段和(最大子段和模板:https://www.luogu.org/problemnew/show/P1115),动态转移方程为:

1 dp[i] = max(a[i], dp[i - 1] + a[i]);

 

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 const int maxn = 100005;
 8 
 9 int dp[maxn], p[maxn];
10 
11 int main(){
12     int n;
13     scanf("%d", &n);
14     for(int i = 1; i <= n; i++)
15         scanf("%d", &p[i]);
16     for(int i = 1; i <= n; i++)
17         dp[i] = max(dp[i - 1] + p[i], p[i]);
18     sort(dp + 1, dp + 1 + n);
19     printf("%d", dp[n]);
20 }
AC代码

 

标签:www,洛谷,show,int,USACO11JAN,P3009,maxn,include,dp
来源: https://www.cnblogs.com/New-ljx/p/11185037.html