PTA 1007 Maximum Subsequence Sum (25 分)

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string.h>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <cassert>
 7 #include <queue>
 8 using namespace std;
 9 int n;
10 int main()
11 {
12         cin >> n;
13
14         vector<int> vec(n);
15         // for(int i = 0 ;i< n;++i)
16         // {
17         //         int x;
18         //         cin >> x;
19         //         vec.push_back(x);
20         // }
21         int temp = 0,left = 0,right = n - 1,sum = -1;
22         int tempidx = 0;
23         for(int i = 0;i < n; ++i)
24         {
25                 cin >> vec[i];
26                 temp += vec[i];
27                 if(temp < 0)
28                 {
29                         temp = 0;
30                         tempidx = i + 1;
31                 }
32                 else if(temp > sum)
33                 {
34                         sum = temp;
35                         right = i;
36                         left = tempidx;
37                 }
38         }
39         if(sum < 0)
40         sum = 0;
41
42         cout << sum << " " << vec[left] << " " << vec[right] << endl;
43 } 

原文地址:https://www.cnblogs.com/Jawen/p/11320338.html

时间: 2024-10-10 12:48:42

PTA 1007 Maximum Subsequence Sum (25 分)的相关文章

pta 1007 Maximum Subsequence Sum (25分)

Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For exampl

[PTA] PAT(A) 1007 Maximum Subsequence Sum (25 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Problem portal: 1007 Maximum Subsequence Sum (25 分) Description Given a sequence of $K$ integers { $N_{1}?$, $N_{2}?$, $...$, $N_{K}$ }. A continuous subsequ

1007 Maximum Subsequence Sum (25分) 求最大连续区间和

1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has th

数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)

Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For exampl

1007 Maximum Subsequence Sum (25 分)

Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For exampl

1007 Maximum Subsequence Sum (25分)(动态规划DP)

#include <vector> #include<iostream> using namespace std; int main() { int k; cin>>k; int left_index=0,right_index=k-1,sum=-1,tmp=0,tmp_index=0; vector <int> num(k); for(int i=0;i<k;i++) { cin>>num[i]; tmp+=num[i]; if(tmp&

1007 Maximum Subsequence Sum (25)(25 分)

1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, ..., N~j~ } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence

1007. Maximum Subsequence Sum (25)——PAT (Advanced Level) Practise

题目信息: 1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <=

5-1 Maximum Subsequence Sum (25分)

Given a sequence of KK integers { N_1N?1??, N_2N?2??, ..., N_KN?K?? }. A continuous subsequence is defined to be { N_iN?i??, N_{i+1}N?i+1??, ..., N_jN?j?? } where 1 \le i \le j \le K1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which