题解 | #编程题2#
编程题2
http://www.nowcoder.com/practice/1aeba6ba677949249aba82d81edc3fea
两种思路
思路1(会报超时)
#include <stdio.h> #include <math.h> #include <algorithm> #include <iostream> using namespace std; using namespace std; int main() { int n; scanf("%d\n",&n); int array[500001]; int i = 0; for (i = 0; i < n; i++) { scanf("%d", &array[i]); } int dp = array[0] * array[0]; int max_v = dp; for (int i = 1; i < n; i++) { int min_add = array[i]; int sum = 0; for (int j = i; j >= 0; j--) { min_add = min(min_add,array[j]); sum += array[j]; max_v = max(max_v, min_add * sum); } dp = max(dp, max_v); } printf("%lld", dp); return 0; }
思路2
#include <stdio.h> #include <math.h> #include <algorithm> #include <iostream> using namespace std; int main() {//输入 int n; int array[500001]; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&array[i]); } int ans = 0; for(int j=100;j>=0;j--) { int sum = 0;int min_num = 101; for(int i=0;i<n;i++) { if(array[i]<j) { sum = 0; min_num = 101; } else { sum += array[i]; min_num = min(min_num,array[i]); ans = max(ans,sum*min_num); } } } printf("%d",ans); return 0; }