01-复杂度2 Maximum Subsequence Sum(25 分)
来源:互联网 发布:软件开发系统架构 编辑:程序博客网 时间:2024/04/29 08:24
Problem:
Given a sequence of KK integers {
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
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
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
#include<bits/stdc++.h>using namespace std;typedef long long ll;ll Start, End;int a[1000006];ll n;int MaxSubseqSum2( int A[], int N ){ int ThisSum, MaxSum = -0xffff; int i, j; for( i = 0; i < N; i++ ) /* i是子列左端位置 */ { ThisSum = 0; /* ThisSum是从A[i]到A[j]的子列和 */ for( j = i; j < N; j++ ) /* j是子列右端位置 */ { ThisSum += A[j]; /*对于相同的i,不同的j,只要在j-1次循环的基础上累加1项即可*/ if( ThisSum > MaxSum ) /* 如果刚得到的这个子列和更大 */ { MaxSum = ThisSum; /* 则更新结果 */ Start = A[i]; End = A[j]; } } /* j循环结束 */ } /* i循环结束 */ return MaxSum;}void input(){ cin >> n; for(ll i = 0; i < n; i++) { cin >> a[i]; }}void output(){ int temp = MaxSubseqSum2(a, n); if(temp < 0) cout << 0 << " " << a[0] << " " << a[n - 1] << endl; else cout << MaxSubseqSum2(a, n) << " " << Start << " " << End << endl;}int main(){ cin.sync_with_stdio(false); input(); output(); return 0;}
- 01-复杂度2 Maximum Subsequence Sum(25 分)
- 01-复杂度2 Maximum Subsequence Sum(25 分)
- 01-复杂度2 Maximum Subsequence Sum(25 分)
- pta 01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分)
- PTA 数据结构 01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分) python + c++
- 01-复杂度2 Maximum Subsequence Sum (25分)
- PAT数据结构_01-复杂度2 Maximum Subsequence Sum (25分)
- 01-复杂度2 Maximum Subsequence Sum (25分)
- 种子
- python linux ubuntu守护程序案例
- Python日志中dictConfig()方法的dict对象模式
- Linux-Bash-seq
- M
- 01-复杂度2 Maximum Subsequence Sum(25 分)
- 浅析VO、DTO、DO、PO的概念、区别和用处
- Detecting Text in Natural Image with Connectionist Text Proposal Network论文笔记
- 黑色星期五
- CentOS6.8上VNC的安装、配置和使用
- STL(十七)string基本字符序列容器
- git常用命令
- 外观模式
- 欢迎使用CSDN-markdown编辑器