PAT 1007
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
动态规划的经典问题,最大连续子序列和。
代码
- 1 #include <stdio.h>
- 2
- 3 int main()
- 4 {
- 5 int K;
- 6 int data[];
- 7 int i;
- 8 int flag;
- 9 while(scanf("%d",&K) != EOF){
- flag = ;
- for (i=;i<K;++i){
- scanf("%d",&data[i]);
- if(data[i] >= )
- flag = ;
- }
- if(flag == ){
- printf("0 %d %d\n",data[],data[K-]);
- continue;
- }
- int max_seq = data[];
- int max_s = ,max_e = ;
- int sum = data[];
- int sum_s = ,sum_e = ;
- for(i=;i<K;++i){
- if(sum < ){
- sum = data[i];
- sum_s = sum_e = i;
- }
- else{
- sum += data[i];
- sum_e = i;
- }
- if(sum > max_seq){
- max_seq = sum;
- max_s = sum_s;
- max_e = sum_e;
- }
- }
- printf("%d %d %d\n",max_seq,data[max_s],data[max_e]);
- }
- return ;
- }
PAT 1007的更多相关文章
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- PAT 1007 素数对猜想(20)
1007 素数对猜想(20 分) 让我们定义dn为:dn=pn+1−pn,其中pi是第i个素数.显然有d1=1,且对于n>1有dn是偶数."素 ...
- PAT——1007. 素数对猜想
让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数.“素数对猜想”认为“存在无穷多对相邻且差为2的素数”. 现给定任意正 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT 1007. 素数对猜想 (20)
让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数."素数对猜想"认为"存在无穷多对相邻且 ...
- 浙大 pat 1007题解
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT 1007 素数对猜想
https://pintia.cn/problem-sets/994805260223102976/problems/994805317546655744 让我们定义 d~n~ 为:d~n~ = p~ ...
- [pat]1007 Maximum Subsequence Sum
经典最大连续子序列,dp[0]=a[0],状态转移dp[i]=max(dp[i-1]+a[i],a[i])找到最大的dp[i]. 难点在于记录起点,这里同样利用动态规划s[i],如果dp[i]选择的是 ...
- PAT 1007 素数对猜想 C语言
让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数.“素数对猜想”认为“存在无穷多对相邻且差为2的素数”. 现给定任意正 ...
随机推荐
- 查看各个release版本 golang的改动
https://golang.org/doc/go1.1 https://golang.org/doc/go1.2 https://golang.org/doc/go1.3 https://golan ...
- uboot里读sd卡内容
1. sd卡升级命令 mmcinit 0 fatload mmc 0:1 0 uzImage.bin 80000 fatload mmc 0:1 1000000 initrd.gz 580000 bo ...
- android捕获ListView中每个item点击事件
转自:http://www.cnblogs.com/pswzone/archive/2012/03/10/2389275.html package com.wps.android; import ...
- [zouxianghui] 清空GridPanel的checkbox选中行
清空GridPanel的checkbox选中行,GridPanel.getSelectionModel().clearSelections();可以清空选中状态
- 玩一个:可以显示任何xml树结构的xaml定义
学习中, 玩一玩. 效果如下.Xaml随后. <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentatio ...
- 配置OpenGL及第一个实例
Windows环境下安装GLUT的步骤:1.将下载的压缩包解开,将得到5个文件2.在“我的电脑”中搜索“gl.h”,并找到其所在文件夹(如果是VS,则应该是其安装目录下面的“VC\PlatformSD ...
- CentOS7 安装98五笔输入法
86版的安装方式,网上找到一堆,折腾了很久才把98版的安装上,记录一下. 从这里下了这个 http://bbs.chinaunix.net/forum.php?mod=viewthread&t ...
- CentOS上安装MyCat-MySQL
1.安装JDK,要求JDK7以上. 2.下载MyCat,地址. 3.解压Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz,到usr/local/ ...
- ·string和date转化以及数据库时间比较
string 转换成dateString date="2014-06-26 10:09:22.819": SimpleDateFormat sdf = new SimpleDate ...
- input file里的JQ change() 事件的只生效一次
文件选择框的onchange事件只在第一次改变时生效,以后再选择文件不会触发onchange事件. 解决方法1:用jQuery的live代替直接使用change. 错误代码: $("#Upl ...