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的素数”. 现给定任意正 ...
随机推荐
- 陈正冲老师讲c语言之声明和定义的区别
什么是定义?什么是声明?它们有何区别? 举个例子: A)int i; B)extern int i;(关于extern,后面解释) 哪个是定义?哪个是声明?或者都是定义或者都是声明?我所教过的学生几乎 ...
- 【转】Android项目中编译 C的模块
原文网址:http://blog.csdn.net/Harrison_zhu/article/details/4057738 Android编译环境本身比较复杂,且不像普通的编译环境:只有顶层目录下才 ...
- Event事件详解
首先提到event,先要明白event的产生,也要先明白焦点,什么是焦点.焦点 : 使浏览器能够区分用户输入的对象,当一个元素有焦点的时候,那么他就可以接收用户的输入. 我们可以通过一些方式给元素设置 ...
- UVA 10585 Center of symmetry
题意:给出一个点集,问这个集合有没有中心点使点集对称,这个点可以是点集中的点也可以不是点集的点. 解法:一开始我枚举每两个点连线的中点……结果T了orz当时也不知道怎么想的…… 将点按横坐标排序,如果 ...
- javascript 的 split用法
var array = "200,400,300".split(","); var nums = [ ]; for (var i=0 ; i< array ...
- FL2440移植Linux2.6.33.7内核
kernel version:2.6.33.7 /linux-2.6.33.7 OS:CentOS 6.4 cross-compilation chain:arm-linux-4.3.2 /usr/l ...
- Python环境变量设置
在Windows环境下安装了python后,为了方便运行.py文件,可以设置环境变量如下: 环境变量位置 添加值 添加后效果 系统变量中的PATH python.exe所在目录,比如D:\Python ...
- Leetcode OJ : Repeated DNA Sequences hash python solution
Total Accepted: 3790 Total Submissions: 21072 All DNA is composed of a series of nucleotides abb ...
- Pyhton 一行代码求Fibonacci第N项
递归定义很简单,效率当然很低下,且极易超出栈空间大小. 这样做纯粹是为了体现python的语言表现力而已, 并没有任何实际意义. def fib(x): return fib(x-1) + fib(x ...
- HIbernate学习笔记(九) hibernate事务并发处理与乐观悲观锁
事务并发处理 一. 数据库的隔离级别:并发性作用. 1. ReadUncommited(未提交读):没有提交就可以读取到数据(发出了Insert,但没有commit就可以读取到.)很少用 2. ...