总结11.8

· · 个人记录

练习

::::::success[T1] :::::info[题目链接] P1115 最大子段和 ::::: :::::info[题意]{open} 给出⼀个⻓度为 的序列 ,选出其中连续且⾮空的⼀段使得这段和 最⼤。 ::::: :::::warning[思路]{open} 连续⾮空的⼦段:⽐如 a[1]~a[3] , a[2]~a[3] , a[2]~a[4] ...... 观察发现,以 a[i] 结尾的最⼤⼦段和只可能有⼀个值。 ::::: :::::warning[动态规划写法]

实际就是推表格

1. 先定义表格 int dp[N];

dp[i]: 以第 i 个数作为结尾的最⼤⼦段和为dp[i]。(状态定义)

注意必须选择第 i 个数。

如果能把dp数组的每⼀个值都求出来,那么答案就是dp数组中的最⼤值,最终的最⼤⼦段和⼀定是以某⼀个数作为结尾的。

2.如何推出表格?(打表)

思考:假设 dp[1]~dp[i-1] 的值都已求出,如何确定 dp[i] 的值呢?

显然,如果 dp[i-1]也就是以第 i-1 个数结尾的最⼤⼦段和是正数,那么 dp[i] = dp[i - 1] + a[i]。

反之,如果 dp[i-1] 是负数,那么 dp[i]= a[i]。
:::::success[正确代码]
```cpp line-numbers
#include<bits/stdc++.h>
#include<math.h>
#include<cmath>
using namespace std;
typedef long long ll;
int a[200005],dp[200005];
int main() {
// freopen("P1115_1.in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,maxl=INT_MIN;
cin>>n;
dp[0]=0;
for(int i=1;i<=n;i++)
cin>>a[i],dp[i]=max(dp[i-1]+a[i],a[i]),maxl=max(maxl,dp[i]);
cout<<maxl;
return 0;
}
```
:::::
::::::

::::::success[T2] :::::info[题目链接] P12889 [蓝桥杯 2025 国 Java B] 情绪链路 ::::: :::::info[题意]{open} 考察最⼤⼦段和的原理,题⽬要求找到最⼤⼦段并将这⼀段的数字都乘以 k,并输出数组的和。 ::::: :::::warning[思路]{open} dp[i]: 以 i 结尾的最⼤⼦段和。

先找出最⼤⼦段和 ans,剩下的数字的和为 sum-ans, 然后再加上 ans*k 就是答案。

cout<<sum-ans+ans*k;

::::: :::::success[正确代码]

#include<bits/stdc++.h>
#include<math.h>
#include<cmath>
using namespace std;
typedef long long ll;
ll a[100005],dp[100005];
int main() {
//  freopen("P1115_1.in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n,maxl=INT_MIN,k,num=0;
    cin>>n>>k;
    dp[0]=0;
    for(ll i=1;i<=n;i++){
        cin>>a[i],dp[i]=max(dp[i-1]+a[i],a[i]);
        num+=a[i];
        if(maxl<dp[i])
            maxl=dp[i];
    }
    cout<<num-maxl+maxl*k;
    return 0;
}
:::::

::::::success[T3] :::::info[题目链接] B3637 最⻓上升⼦序列 ::::: :::::info[题意]{open} ⼀个数的序列b_i,

当 b_1 < b_2 < ... < b_S 的时候,我们称这个序列是上升的。对于给定的⼀个序列 (a_1, a_2, ..., a_N) ,

我们可以得到⼀些上升的⼦序列 (a_{i1},a_{i2}, ..., a_{iK}),

这⾥1 \le i_1 < i_2 < ... < i_K \le N。

⽐如,对于序列 (1, 7, 3, 5, 9, 4, 8) ,有它的⼀些上升⼦序列,

如(1, 7),(3, 4, 8 )等等。这些⼦序列中和最⼤为18,为⼦序列 (1, 3, 5, 9) 的和。

你的任务,就是对于给定的序列,求出最⼤上升⼦序列和。

注意,最⻓的上升⼦序列的和不⼀定是最⼤的。⽐如序列(100,1,2,3)的最⼤上升⼦序列和为100,⽽最⻓上升⼦序列为(1,2,3)。

::::: :::::warning[思路]{open} 思路和上道题如出⼀辙

::::: :::::success[正确代码] ```cpp line-numbers #include<bits/stdc++.h> #include<math.h> #include<cmath> using namespace std; typedef long long ll; int a[200005],dp[200005]; int main() { // freopen("P1115_1.in","r",stdin); // freopen(".out","w",stdout); ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,ans=INT_MIN; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++){ dp[i]=1; for(int j=1;j<i;j++) if(a[j]<a[i]) dp[i]=max(dp[i],dp[j]+1); ans=max(ans,dp[i]); } cout<<ans; return 0; } ``` ::::: :::::: ::::::success[T4] :::::info[题目链接] [P2008 ⼤朋友的数字](https://www.luogu.com.cn/problem/P2008?contestId=289982) ::::: :::::info[题意]{open} 求最⻓不下降⼦序列中所有数之和 ::::: :::::success[正确代码] ```cpp line-numbers #include<bits/stdc++.h> #include<math.h> #include<cmath> using namespace std; typedef long long ll; int a[200005],dp[200005],s[200005]; int main() { // freopen("P1115_1.in","r",stdin); // freopen(".out","w",stdout); ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,ans=INT_MIN; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++){ dp[i]=1; s[i]=a[i]; for(int j=1;j<i;j++) if(a[j]<=a[i]&&dp[j]+1>dp[i]) dp[i]=dp[j]+1,s[i]=s[j]+a[i]; cout<<s[i]<<" "; } return 0; } ``` ::::: :::::: ::::::success[T5] :::::info[题目链接] [P12175 [蓝桥杯 2025 省 Python B] 园艺](https://www.luogu.com.cn/problem/P12175?contestId=289982) ::::: :::::warning[思路]{open} 先枚举间隔,假设间隔为d。 ::::: :::::success[正确代码]{open} ## 方法1: ```cpp line-numbers #include<bits/stdc++.h> using namespace std; const long long N = 5005; long long dp[N][N]; long long obj[N]; int main() { long long n; cin>>n; for(long long i=1;i<=n;i++) cin>>obj[i]; long long ans=0; for(long long i=1;i<=n;i++){ for(long long j=1;j<=n;j++){ dp[i][j]=1; if(i-j<=0) continue; if(obj[i-j]<obj[i]) dp[i][j]=dp[i-j][j]+1; ans=max(ans,dp[i][j]); } } cout<<ans; return 0; } ``` ## 方法2: ```cpp line-numbers #include<bits/stdc++.h> using namespace std; #define endl '\n' typedef long long ll; const int N = 5e3 + 10; int n, m, a[N], ans; int dp[N][N]; // 以i结尾间距为j的LIS int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { for (int d = 1; d <= n; d++) { dp[i][d] = 1; if (i - d <= 0) continue; if (a[i] > a[i-d]) { dp[i][d] = dp[i-d][d] + 1; } ans = max(ans, dp[i][d]); } } cout << ans; return 0; } ``` ::::: ::::::