当前位置:网站首页>1007 Maximum Subsequence Sum (25分)
1007 Maximum Subsequence Sum (25分)
2022-08-09 10:48:00 【Cutecumber】
1007 Maximum Subsequence Sum (25分)
Given a sequence of K integers { N1, N2 , …, NK}. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj} where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
一定要读题啊If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
我用动态规划做的话会部分错误,估计是子序列的定位出了问题,我暂时还没检查出来,以后有空再改吧,现在忙着刷题。二重循环暴力搜索没问题,就是耗时有点长。
#include <iostream>
using namespace std;
void MaxSubSequence(const int A[], int N){
int ThisSum,MaxSum,i,j;
int start=0, end=0;
MaxSum = -1;
for(i=0;i<N;i++)
{
ThisSum = 0;
for(j=i;j<N;j++)
{
ThisSum += A[j];
if(ThisSum > MaxSum){
MaxSum = ThisSum;
start = i;
end = j;
}
}
}
if(MaxSum < 0){
cout << 0 <<' '<<A[0]<<' '<<A[N-1] <<endl;
}
else
cout << MaxSum<<' '<<A[start]<<' '<<A[end];
}
void dp(const int A[], int N){
int ThisSum, MaxSum=-1;
int start = 0, end=0;
int count = 0;
for(int i=0; i<N; i++){
count++;
ThisSum += A[i];
if(ThisSum > MaxSum){
MaxSum = ThisSum;
end = i;
start = i - count + 1;
}
else if(ThisSum < 0){
ThisSum = 0;
count = 0;
}
}
if(MaxSum < 0){
cout << 0 <<' '<<A[0]<<' '<<A[N-1] <<endl;
}
else
cout << MaxSum<<' '<<A[start]<<' '<<A[end] << endl;
}
int main(){
int n;
int list[10002];
cin >> n;
for(int i=0; i<n; i++){
cin >> list[i];
}
MaxSubSequence(list, n);
return 0;
}
边栏推荐
- For versions corresponding to tensorflow and numpy, report FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecate
- Dialogue with the DPO of a multinational consumer brand: How to start with data security compliance?See you on 8.11 Live!
- String类型的字符串对象转实体类和String类型的Array转List
- 日期工具类
- 深度学习--循环神经网络(Recurrent Neural Network)
- UNIX Environment Programming Chapter 15 15.5FIFO
- Invisible OOM in kubernetes
- 985毕业,工作3年,分享从阿里辞职到了国企的一路辛酸和经验
- 数据存储:对dataframe类,使用to_csv()将中文数据写入csv文件
- OpenSSF's open source software risk assessment tool: Scorecards
猜你喜欢

研发需求的验收标准应该怎么写? | 敏捷实践

批量转换经纬度的网页实现方法

对话跨国消费品牌DPO:数据安全合规从何做起?8.11直播见!

CSDN的markdown编辑器语法完整大全

备战金三银四:如何成功拿到阿里offer(经历+面试题+如何准备)

想了解API接口,这一篇就够了

Electron application development best practices

商业技术解决方案与高阶技术专题 - 数据可视化专题

Dialogue with the DPO of a multinational consumer brand: How to start with data security compliance?See you on 8.11 Live!

Netscope: Online visualization tool for neural network structures
随机推荐
多商户商城系统功能拆解26讲-平台端分销设置
OpenSSF的开源软件风险评估工具:Scorecards
activemq message persistence
Product Quantization (PQ)
我用开天平台做了一个定时发送天气预报系统【开天aPaaS大作战】
Unix System Programming Chapter 15 15.2 Pipes
深度学习--循环神经网络(Recurrent Neural Network)
深度学习--生成对抗网络(Generative Adversarial Nets)
解决1.tensorflow运行使用CPU不使用GPU 2.tensorflow环境下的GPU版本号 3.tensorflow和cuda以及cudnn版本对应问题 4.查看cuda和cudnn版本
使用pip成功安装某个库,但pycharm中找不到,此问题的解决方案
caffe ---make all editing error
Quartz分布式实现
pip common commands and changing source files
numpy库中的函数 bincount() where() diag() all()
强化学习 (Reinforcement Learning)
Since I use the HiFlow scene connector, I don't have to worry about becoming a "dropper" anymore
shell脚本实战(第2版)/人民邮电出版社 脚本1 在PATH中查找程序
Transformer+Embedding+Self-Attention原理详解
真香!肝完Alibaba这份面试通关宝典,我成功拿下今年第15个Offer
WUSTOJ:n个素数构成等差数列