当前位置:网站首页>Pat class A - 1007 maximum subsequence sum
Pat class A - 1007 maximum subsequence sum
2022-06-23 01:20:00 【S atur】
The question : Nothing wrong , Is the problem of finding the sum of a maximal subsequence .
Ideas : Two ideas —— violence 、dp. Violence uses prefix sum to find the maximum difference , But it's still a surprise 1e4 The complexity of is supposed to time out , But the data of this question may be more water 400ms The limit of .
Violence code :
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5+10;
int n, a[N], pre[N], ne;
signed main()
{
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
pre[i] = pre[i-1]+a[i];
if(a[i]<0) ne ++;
}
if(ne==n){
cout << 0 << " " << a[1] << " " << a[n] << endl;
return 0;
}
int ans = 0, bg = 0, ed = 0;
for(int i = 1; i <= n; i ++){
for(int j = i; j <= n; j ++){
int sum = pre[j]-pre[i-1];
if(sum>ans){
ans = sum;
bg = a[i], ed = a[j];
}
}
}
cout << ans << " " << bg << " " << ed << endl;
return 0;
}
dp Code :
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5+10;
int n, a[N];
signed main()
{
cin >> n;
int ans = -1, sum = 0, idx = 1;
int bg = 1, ed = n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
sum += a[i];
if(sum<0){
idx = i+1;
sum = 0;
}
else if(sum>ans){
ans = sum;
bg = idx;
ed = i;
}
}
cout << max(0ll, ans) << " " << a[bg] << " " << a[ed] << endl;
return 0;
}
边栏推荐
- Prevent others from using the browser to debug
- OSPF comprehensive experiment
- The longest child sequence of the 2019 Blue Bridge Cup
- 基于深度学习的视觉目标检测技术综述
- Phantomjs Usage Summary
- Learn the specific technical learning experience of designing reusable software from the three levels of class, API and framework
- graphite statsd接口数据格式说明
- How to set the power-off auto start of easycvr hardware box
- C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)
- 62. different paths
猜你喜欢

How about precious metal spot silver?

Quelle est la structure et la façon dont les données sont stockées dans la base de données?

E-R diagram

Dig three feet to solve the data consistency problem between redis and MySQL

Overview of visual object detection technology based on deep learning

Tidb monitoring upgrade: a long way to solve panic

SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications

three. JS simulated driving tour art exhibition hall - creating super camera controller

How to calculate the position of gold ETF

Read Amazon memorydb database based on redis
随机推荐
C# SerializableDictionary序列化/反序列化
Cadence spb17.4 - Allegro - optimize and specify the polyline connection angle of a single electrical line - polyline to arc
Xiaobai operates win10 to expand Disk C (allocate disk D memory to Disk C) and the test is valid for many times
What aspects of language and database knowledge are needed to build a web Kanban!
JS prevent the PC side from copying correct links
OSPF comprehensive experiment
MGRE环境下的OSPF实验
The road of architects starts from "storage selection"
工程目录导航
SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications
Binary tree to string and string to binary tree
E-R diagram
Tidb monitoring upgrade: a long way to solve panic
B tree and b+ tree
LINQ 查询
Have you stepped on these pits? Use caution when creating indexes on time type columns
SAP mm me27 create intra company sto order
Daily question brushing record (I)
SFOD:无源域适配升级优化,让检测模型更容易适应新数据
Vector 6 (inheritance)