mirror of
https://github.com/hao14293/2021-Postgraduate-408.git
synced 2026-02-02 18:20:30 +08:00
Create 01-复杂度2 Maximum Subsequence Sum (25 分).cpp
This commit is contained in:
24
ZJU_MOOC_数据结构/01-复杂度2 Maximum Subsequence Sum (25 分).cpp
Normal file
24
ZJU_MOOC_数据结构/01-复杂度2 Maximum Subsequence Sum (25 分).cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int main() {
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
vector<int> v(n);
|
||||
int leftindex = 0, rightindex = n - 1, sum = -1, temp = 0, tempindex = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
scanf("%d", &v[i]);
|
||||
temp = temp + v[i];
|
||||
if (temp < 0) {
|
||||
temp = 0;
|
||||
tempindex = i + 1;
|
||||
} else if (temp > sum) {
|
||||
sum = temp;
|
||||
leftindex = tempindex;
|
||||
rightindex = i;
|
||||
}
|
||||
}
|
||||
if (sum < 0) sum = 0;
|
||||
printf("%d %d %d", sum, v[leftindex], v[rightindex]);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user