pat 1007. Maximum Subsequence Sum (25)
2016-05-11 14:13
429 查看
1007. Maximum Subsequence Sum (25)
时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueGiven 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 subsequenceis 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 -21Sample Output:
10 1 4
#include<stdio.h>#include<string.h>#include<math.h>#define max 10010int main(){int k,ans[max];memset(ans,0,sizeof(ans));scanf("%d",&k);int kase=0;for(int i=0;i<k;i++){scanf("%d",&ans[i]);if(ans[i]>=0) kase=1;}int sum=0,left,right;for(int i=0;i<k;i++){int thissum=0;if(ans[i]>=0){for(int j=i;j<k;j++){thissum+=ans[j];if(thissum>sum||(sum==0&&thissum==0)){left=i;right=j;sum=thissum;}}}}if(sum==0){if(kase==0)printf("0 %d %d\n",ans[0],ans[k-1]);elseprintf("0 0 0\n");}elseprintf("%d %d %d\n",sum,ans[left],ans[right]);return 0;}
相关文章推荐
- 优化duilib中的CDateTimeUI控件-修正XP下无法使用的问题
- Hue 可视化的Hadoop架构工具
- 线程中更新 UILabel的text
- org.hibernate.PropertyAccessException: Null value was assigned to a property。。。。完美解决
- NGUI之UIGrid & UITable
- String、StringBuilder、 StringBuffer 深入分析 源码解析
- Elasticsearch之API CUED操作
- IOS 开发中 TableView的文本Cell高度的自适应,UILabel自动换行适应
- Java中String、StringBuilder、 StringBuffer的理解
- Android UI线程
- 将color转为UIImage
- iOS利用九切片进行切图UI不会变形
- UIBarButtonSystemItemFixedSpace
- 【Arduino官方教程第一辑】示例程序 3-6 LED亮度渐隐
- Concurrent包中Queue(2)----ArrayBlockingQueue
- Concurrent包中Queue(1)----LinkedBlockingQueue
- druid 应用到项目中
- StringBuffer、StringBuilder源码分析
- 最长递增子序列详解(longest increasing subsequence)(by joylnwang)
- Arduino携手LCD1602 向世界问好“hello World!”