题目要求找到数组中某一段最小值与该段所有值和的乘积的最大值。利用动态规划,将问题分解为将数组截取三段,取其最大值,分别为全部值、去掉数组最小值后左半部分、去掉数组最小值右左半部分。AC80,超时
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <time.h>
#include <vector>
#include <list>
#include<iomanip>
#include<time.h>
#define N_MAX 205
#define INF (0x3f3f3f3f)
using namespace std;
long long Data[100001] = { 0 };
bool Flag[100001] = { 0 };
int N_min = 0;
int N;
int find_min(int s, int e)
{
int M = INF, k =s;
for (int i = s; i <= e; ++i)
{
if (Data[i] < M)
{
M = Data[i];
k = i;
//cout << i;
}
}
return k;
}
long long Cal(int s, int e)
{
if (s > e )
return 0;
if (s == e)
return Data[s] * Data[s];
long long M = 0;
for (int i = s; i <= e; ++i)
{
M += Data[i];
}
int k = find_min(s, e);
M = M * Data[k];
//cout << M << endl;
M = max(max(Cal(s, k - 1), Cal(k + 1, e)), M);
return M;
}
int main()
{
cin >> N;
for (int i = 0; i < N; ++i)
{
cin >> Data[i];
}
cout << Cal(0, N);
return 0;
}