洛谷 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.cnblogs.com/New-ljx/p/11185037.html
0条评论
添加一条新回复