当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
可能95%的人还在犯的PyTorch错误
【原创】VMware Workstation实现Openwrt软路由功能,非ESXI,内容非常详细!
对话跨国消费品牌DPO:数据安全合规从何做起?8.11直播见!
PoseNet: A Convolutional Network for Real-Time 6-DOF Camera Relocalization Paper Reading
Quartz分布式实现
shell脚本实战(第2版)/人民邮电出版社 脚本1 在PATH中查找程序
How tall is the B+ tree of the MySQL index?
2022年台湾省矢量数据(点线面)及数字高程数据下载
centos7.5 设置Mysql开机自启动
MNIST机器学习入门
随机推荐
Unix Environment Programming Chapter 15 15.7 Message Queuing
tensorflow和numpy对应的版本,报FutureWarning: Passing (type, 1) or ‘1type‘ as a synonym of type is deprecate
对话跨国消费品牌DPO:数据安全合规从何做起?8.11直播见!
性能测试(05)-表达式和业务关联-json关联
shell脚本实战(第2版)/人民邮电出版社 脚本1 在PATH中查找程序
我用开天平台做了一个定时发送天气预报系统【开天aPaaS大作战】
[Error record] Solve the problem that ASRock J3455-ITX cannot be turned on without a monitor plugged in
AQS同步组件-ForkJoin、BlockingQueue阻塞队列解析和用例
通过Doc在MySQL数据库中建表
Quartz分布式实现
verbose np.matmul/np.dot/np.multiply/tf.matmul/tf.multiply/*
ESIM(Enhanced Sequential Inference Model)- 模型详解
TensorFlow—计算梯度与控制梯度 : tf.gradients和compute_gradients和apply_gradients和clip_by_global_norm控制梯度
Electron application development best practices
Unix System Programming Chapter 15 15.2 Pipes
2022强网杯WP
机器学习--线性回归(Linear Regression)
unix环境编程 第十五章 15.9 共享存储
jmeter BeanShell 后置处理器
用Word写代码