1. 程式人生 > >【笨方法學PAT】1007 Maximum Subsequence Sum(25 分)

【笨方法學PAT】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 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

二、題目大意

給一個數組,求和最大的連續子序列;如果最大和為0,輸出0、第一位和最後一位。

三、考點

最大連續子序列和

四、解題思路

自己的方法比較笨,思路是這樣的:

1、直接暴力求解,O(n^3) 肯定會超時,估計這種方案可以一半分數;

2、優化,儲存陣列的和,之後對和二次遍歷,sum[i] - sum[j] > ans_sum,就更新ans_sum、ans_left、ans_right;

五、演算法優化

複雜度O(n^3):窮舉所有可能的首尾子串組合,用i和j迴圈確定首尾,k迴圈累加這之間的數,如果這個組合的累加大於記錄過的maxSum則更新。**

複雜度O(n^2):窮舉優化版,只用i迴圈確定子串的首個數,j迴圈不斷延長這個子串,延長的同時累加,如果在累加的某一刻大於了maxSum則更新。

複雜度O(n^2):分治法,將序列分成左右兩部分,分別遞迴求出左,右後,再求跨越中界的最大子序列和。求跨越序列時一直從中間向左/右走到底的同時累加,如果在累加的某一刻大於了maxSum則更新。

複雜度O(n):聯機演算法,只掃描一遍序列,掃描的同時做累加。判斷累加當前元素後的thisSum,如果比記錄過的maxSum大則更新,如果比maxSum小且為負,那麼丟棄構成thisSum的子串,從下一個元素開始另起一個子串。

六、程式碼

#include<iostream>
#include<vector>
using namespace std;
int main() {
	//讀入資料
	int k;
	cin >> k;
	vector<int> vec(k);
	for(int i=0;i<k;++i){
		cin >> vec[i];
	}

	//直接二次遍歷,每次都要求和,估計會超時
	vector<int> sum(k+1);
	sum[0] = 0;
	for (int i = 1; i <= k; ++i)
		sum[i] = sum[i - 1] + vec[i-1];

	//求解
	int ans_sum=-9999999, ans_left, ans_right;
	for (int i = 0; i <= k; ++i) {
		for (int j = 0; j < i; ++j) {
			if (sum[i] - sum[j] > ans_sum) {
				ans_sum = sum[i] - sum[j];
				ans_left = j;
				ans_right = i;
			}
		}
	}

	//輸出
	if (ans_sum < 0)
		cout << "0 " << vec[0] << " " << vec[k - 1];
	else 
		cout << ans_sum << " " << vec[ans_left] << " " << vec[ans_right-1];
		
	system("pause");
	return 0;
}

七、聯機演算法

解題思路:在讀取資料的過程中,計算sum,如果sum<0,捨棄,重新開始計數;如果sum大,更新輸出。

#include<iostream>
#include<vector>
using namespace std;
int main() {
	//讀入資料
	int k;
	cin >> k;
	vector<int> vec(k);
	int ans_sum=-1, ans_left=0, ans_right=k-1, tmp_sum=0, tmp_left=0;
	for(int i=0;i<k;++i){
		cin >> vec[i];
		tmp_sum += vec[i];
		//捨棄,重新開始
		if (tmp_sum < 0) {
			tmp_sum = 0;
			tmp_left = i + 1;
		}
		//和增大,更新輸出
		else if (tmp_sum > ans_sum) {
			ans_sum = tmp_sum;
			ans_right = i;
			ans_left = tmp_left;
		}
	}

	//輸出
	if (ans_sum < 0)
		cout << "0 " << vec[0] << " " << vec[k - 1];
	else 
		cout << ans_sum << " " << vec[ans_left] << " " << vec[ans_right];
		
	system("pause");
	return 0;
}

相關推薦

法學PAT1007 Maximum Subsequence Sum25

一、題目 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​​ 

法學PAT1013 Battle Over Cities25

一、題目 It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/towa

法學PAT1012 The Best Rank25

一、題目 To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programmin

法學PAT1009 Product of Polynomials25

一、題目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each input

法學PAT1149 Dangerous Goods Packaging 25

一、題目 When shipping goods with containers, we have to be careful not to pack some incompatible goods into the same container, or we might get ourse

法學PAT1150 Travelling Salesman Problem 25

一、題目 The "travelling salesman problem" asks the following question: "Given a list of cities and the distances between each pair of cities, what is

未完成法學PAT1060 Are They Equal 25

一、題目 If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0

法學PAT1044 Shopping in Mars 25

一、題目 Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). Whe

法學PAT1074 Reversing Linked List 25

一、題目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L.

法學PAT1113 Integer Set Partition 25

一、題目 Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A​1​​ and A​2​

法學PAT1036 Boys vs Girls 25

一、題目 This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female stud

法學PAT1011 World Cup Betting20

一、題目 With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams

未完成法學PAT1018 Public Bike Management 30

一、題目 There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike

PAT甲級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​​ } wher

1007 Maximum Subsequence Sum25 最大連續子序列和

題意:求最大連續子序列和並記錄該序列的頭尾元素 #include <bits/stdc++.h> using namespace std; int N; int main() { cin>>N; vector<int>

PAT--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​

1007 Maximum Subsequence Sum25 PAT甲級

Problem Description: Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+

PAT (Advanced Level) 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

PAT甲級真題(動態規劃)——1007 Maximum Subsequence Sum 25

1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1, N​2 , …, NK }. A continuous subsequence is defined to be { N​i, N​i+

1007 Maximum Subsequence Sum 25

1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuou