最大连续子序列
来源:互联网 发布:php extension dir 编辑:程序博客网 时间:2024/05/16 05:12
Total Submission(s) : 2 Accepted Submission(s) : 2
Problem Description
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., <br>Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个, <br>例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和 <br>为20。 <br>在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该 <br>子序列的第一个和最后一个元素。<br>
Input
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。<br>
Output
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元 <br>素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。 <br>
Sample Input
6-2 11 -4 13 -5 -210-10 1 2 3 4 -5 -23 3 7 -2165 -8 3 2 5 01103-1 -5 -23-1 0 -20
Sample Output
20 11 1310 1 410 3 510 10 100 -1 -20 0 0<div style='font-family:Times New Roman;font-size:14px;background-color:F4FBFF;border:#B7CBFF 1px dashed;padding:6px'><div style='font-family:Arial;font-weight:bold;color:#7CA9ED;border-bottom:#B7CBFF 1px dashed'><i>Hint</i></div>Hint</div>Huge input, scanf is recommended.
Source
浙大计算机研究生复试上机考试-2005年
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#include <iomanip>#define maxn 105#define MAXN 10005#define mod 1000000007#define INF 0x3f3f3f3f#define exp 1e-6 #define pi acos(-1.0)using namespace std;int dp[MAXN]={0},maxx,sum=0,s=0;int MAX(int a,int b){return a>b;} int main(){ //freopen("D:\\a.txt","r",stdin); ios::sync_with_stdio(false); int t,n,i,m,f,e,cur; while(cin>>n&&n) { for(i=0;i<n;i++)cin>>dp[i];maxx=dp[0];sum=0;f=0,e=0,cur=0;for(i=0;i<n;i++) { sum=sum+dp[i];if(maxx<sum){maxx=sum;f=cur;e=i;}if(sum<0){sum=0;cur=i+1;} } if(maxx<0) cout<<"0 "<<dp[0]<<' '<<dp[n-1]<<endl; else cout<<maxx<<' '<<dp[f]<<' '<<dp[e]<<endl;} return 0; }
阅读全文
1 0
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 简单的Java程序
- C#中的值类型和引用类型
- 数据特征提取,特征量相关性分析
- AngularJs小案例_列表
- F-散度(F-divergence)
- 最大连续子序列
- spring常用的几个aware bean接口:
- 重装ubuntu后的第二天——openCV库的安装
- 多对多 二级缓存
- HDU 4267 (树状数组)
- 七夕节
- Python如何给文件按顺序重命名
- 【卡尔曼滤波经典讲解,C++算法实现】
- BLE