Maximum Subsequence Sum(With C)
来源:互联网 发布:vs2005操作mysql 编辑:程序博客网 时间:2024/05/18 03:16
Perface
This is my first CSDN blog, and I decided to write this blog entirely because of a blog called “Even though you don’t have a reader, you should write your own blog“Then I began my first blog with a homework in my data structure course.
Task Description
Given a sequence of K 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 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
PAY ATTENTIONS The output is the elements not the indices!!!
My Solution
`#include<stdio.h>int main(){ int ThisSum,MaxSum,i,j,begin,best_begin,end; int N; int A[10000]; scanf("%d",&N); for(j=0;j<N;j++){ scanf("%d",&A[j]); } ThisSum=0; MaxSum=best_begin=-1;/*pay attention to the initialization of MaxSum*/ begin=end=0; for(j=0;j<N;j++){ ThisSum+=A[j]; if(ThisSum>MaxSum){ MaxSum=ThisSum; end=j; best_begin=begin; } else if(ThisSum<0) { ThisSum=0; begin=j+1; } } if(best_begin==-1){ MaxSum=0; printf("%d %d %d",MaxSum,A[0],A[N-1]); } else{ printf("%d %d %d",MaxSum,A[best_begin],A[end]); } return 0;}
Comments
It should be clear why the time bound is correct, but it takes a little thought to see why the algorithm actually works. This is left to the reader. An extra advantage of this algorithm is that it makes only one pass through the data, and once a[i] is read and processed, it does not need to be remembered. Thus, if the array is on a disk or tape, it can be read sequentially, and there is no need to store any part of it in main memory. Furthermore, at any point in time, the algorithm can correctly give an answer to the subsequence problem for the data it has already read (the other algorithms do not share this property). Algorithms that can do this are called on-line algorithms. An on-line algorithm that requires only constant space and runs in linear time is just about as good as possible.
- Maximum Subsequence Sum(With C)
- 1007. Maximum Subsequence Sum (25)[C语言]
- Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- PAT1007. Maximum Subsequence Sum
- PAT_1007: Maximum Subsequence Sum
- 1007 Maximum Subsequence Sum
- PAT - Maximum Subsequence Sum
- PAT1007. Maximum Subsequence Sum
- Maximum Subsequence Sum (25)
- Maximum Subsequence Sum
- 1007.Maximum Subsequence Sum
- Maximum Subsequence Sum
- pat1007 Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- Maximum Subsequence Sum
- Ubuntu配置SDK和AVD
- Gradle理论知识小结
- linux中free命令详解
- 5.Python深入_装饰器
- ViewPage实现Tab 底部导航 (如微信,微票儿主界面)
- Maximum Subsequence Sum(With C)
- Markdown 文字编辑器介绍
- ,,,,,ddfd
- Hibernate5 demo
- Hibernate5 demo
- Win7安装python第三方模块objgraph报错<UnicodeEncodeError: 'ascii' codec can't encode character u'\u258f'>
- .NET4.0,C#泛型接口的协变与抗变
- Vim 自动补全插件 YouCompleteMe 安装与配置
- 基于python3,抓取韩寒博客文章