题解 P1115 最大子段和
link
容易想到朴素做法:
for (int l = 1; i <= n; ++i) {
for (int r = 1; j <= n; ++j) {
int v = s[r] - s[l - 1];
ans = max(ans, v);
}
}
但是显然
再回头看代码:想要 v
最大,只需要
于是我们可以
int m[N];
for (int i = 1; i <= n; ++i) {
m[i] = min(m[i - 1], s[i]);
}
然后随
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], m[N], s[N];
int n;
int main()
{
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i)
{
cin >> a[i];
s[i] = s[i - 1] + a[i];
m[i] = min(m[i - 1], s[i]);
}
int ans = -2e4;
for(int i = 1; i <= n; ++i)
{
ans = max(ans, s[i] - m[i - 1]);
}
cout << ans;
return 0;
}//ACed
但是显然这份代码可以优化。
优化
1.
我们发现求完前缀和后,
于是可以优化掉
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int m[N], s[N];
int n;
int main()
{
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i)
{
int t; cin >> t;
s[i] = s[i - 1] + t;
m[i] = min(m[i - 1], s[i]);
}
int ans = -2e4;
for(int i = 1; i <= n; ++i)
{
ans = max(ans, s[i] - m[i - 1]);
}
cout << ans;
return 0;
}//ACed
2.
两个循环都是
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int m[N], s[N], ans = -2e4;
int n;
int main()
{
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i)
{
int t; cin >> t;
s[i] = s[i - 1] + t;
m[i] = min(m[i - 1], s[i]);
ans = max(ans, s[i] - m[i - 1]);
}
cout << ans;
return 0;
}//ACed
3.
发现只需要用到
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int mi, s[N], ans = -2e4;
int n;
int main()
{
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i)
{
int t; cin >> t;
s[i] = s[i - 1] + t;
ans = max(ans, s[i] - mi);
mi = min(mi, s[i]);
}
cout << ans;
return 0;
}
4.
同理,只需要用到
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int mi, s, ans = -2e4;
int n;
int main()
{
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i) {
int t; cin >> t;
s += t;
ans = max(ans, s - mi);
mi = min(mi, s);
}
cout << ans;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!