數據結構(陳越) 作業題 第一周


1-1 最大子列和問題 20pts

時間限制
10000 ms
內存限制
65536 kB
代碼長度限制
8000 B
判題程序
Standard

給定K個整數組成的序列{ N1, N2, ..., NK },“連續子列”被定義為{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。“最大子列和”則被定義為所有連續子列元素的和中最大者。例如給定序列{ -2, 11, -4, 13, -5, -2 },其連續子列{ 11, -4, 13 }有最大的和20。現要求你編寫程序,計算給定整數序列的最大子列和。

輸入格式:

輸入第1行給出正整數 K (<= 100000);第2行給出K個整數,其間以空格分隔。

輸出格式:

在一行中輸出最大子列和。如果序列中所有整數皆為負數,則輸出0。

輸入樣例:
6
-2 11 -4 13 -5 -2
輸出樣例:
20


此題在姥姥的視頻里有講,一共講了4種方法,其中最優的是在線算法,此處我不再贅述,直接貼上代碼,詳情請看姥姥的視頻講解。
 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int N;
 7     cin >> N;
 8 
 9     int this_sum=0;
10     int max_sum=0;
11     int element=0;
12 
13     for (int i=0;i<N;i++)
14     {
15         cin >> element;
16         this_sum += element;
17         if (this_sum > max_sum)
18             max_sum = this_sum;
19         if (this_sum < 0)
20             this_sum = 0;
21     }
22 
23     if (max_sum<0)
24         cout << 0;
25     else
26         cout << max_sum;
27 
28     return 0;
29 }

 

 

1-2. Maximum Subsequence Sum

時間限制
400 ms
內存限制
65536 kB
代碼長度限制
8000 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 <= 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

這題與上一題類似,只不過多出了一個要求:輸出最大子列的首尾元素。此題我的做法是,用兩個變量l_temp和r_temp記錄當前子列的首尾元素,如果該子列的和比當前記錄的最大和要大,則用這兩個變量更新最大子列的首尾元素l_max和r_max。如果當前子列的和小於0,則從下一個元素開始重新計算。但是有幾個特殊點需要注意:

1.如果輸入的元素全為負數,則最大和為0,同時輸出首尾的負數。
2.如果輸入元素全為負數和0,則最大和為0,但輸出應該是0 0 0.
比如輸入-3 0 -6,輸出是0 0 0,而不是0 -3 -6
3.輸出的首尾元素應該具有最小下標,比如:
輸入3 7 -10 11,則應輸出11 3 11,而不是11 11 11

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int N;
 7     cin >> N;
 8 
 9     int this_sum=0;
10     int max_sum=0;
11     int l_max=0;
12     int r_max=0;
13     int l_temp=0;
14     int r_temp=0;
15     bool begin_again=false;//判斷this_sum是否重新開始
16     bool allnegative=true;//判斷數組元素是否全為0
17 
18     int *element = new int [N];
19     for (int i=0;i<N;i++)
20     {        
21         cin >> element[i];
22     }
23 
24     l_temp=element[0];
25     r_temp=element[0];
26     for (int i=0;i<N;i++)
27     {        
28         this_sum += element[i];
29 
30         if (element[i]>=0)
31             allnegative=false;
32 
33         if (begin_again)
34         {
35             l_temp=element[i];
36             r_temp=element[i];
37             begin_again=false;
38         }
39         else
40         {
41             r_temp=element[i];
42         }
43 
44         if (this_sum > max_sum)
45         {
46             max_sum = this_sum;
47             l_max = l_temp;
48             r_max=r_temp;
49         }
50         else
51             if (this_sum < 0)//只能用<0,不能<=0,因為要輸出smallest indices,若3 7 -10 11,則應輸出11 3 11,而不是11 11 11
52             {
53                 this_sum = 0;
54                 begin_again=true;//重新開始
55             }
56     }
57 
58     if (max_sum==0)
59         if (allnegative)
60             cout << 0  << ' ' << element[0] << ' ' << element[N-1] << endl;
61         else
62             cout << 0 << ' ' << 0 << ' ' << 0 << endl;
63     else
64         cout << max_sum << ' ' << l_max << ' ' << r_max << endl;
65 
66     return 0;
67 }
 
          

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM