3768

# 网易互联网

6
1 3 2 4 6 5

0 1 2 3 4 5
0 1 3 3 4 6 6
1 3 3 4 6 6
2 2 4 6 6
3 4 6 6
4 6 6
5 5

## 3.代码

``````#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define M 100
#define N 100
int dp[M][N];

void fullTheDP(vector <int> num)
{
int len = num.size();
//对角线上的元素为num中每个数本身的值
for (int i = 0; i < len; i++)
dp[i][i] = num[i];
//只需填充dp矩阵的右上角即可，dp[i][j]表示从num中从序号i到序号j的子序列的最大值
for (int i = 0; i < len - 1; i++)
for (int j = i + 1; j < len; j++)
dp[i][j] = max(dp[i][j - 1], dp[j][j]);
return;
}

void findTheMinOfMaxOfK(int k, int len)
{
vector <int> max;
for (int i = 0; i < len - k + 1; i++)
{
max.push_back(dp[i][i + k - 1]);
}
sort(max.begin(), max.end());
cout << max[0] << ' ';
}

int main()
{
//处理输入
int n;
cin >> n;
int i = n;
vector <int> num;
while (i--)
{
int tmp;
cin >> tmp;
num.push_back(tmp);
}
//填充dp矩阵
memset(dp, 0, sizeof(dp));
fullTheDP(num);
//查询并输出
int k = 1;
while (k <= n)
{
findTheMinOfMaxOfK(k, n);
k++;
}
cout << endl;
return 0;
}
``````

# 声网

## 1.题目

6
-2,11，-4,13，-5,-2

20

## 3.代码

``````#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define M 100
#define N 100
int dp[M][N];

void findTheMaxSumSeq(vector <int> num)
{
//填充dp
int len = num.size();
int maxNum =num[0];
int ibegin = 0, iend = 0;

//只需填充dp矩阵的右上角即可，对角线上的元素为num中每个数本身的值，dp[i][j]表示从num中从序号i到序号j的子序列的最大值
for (int i = 0; i < len ; i++)
for (int j = i ; j < len; j++)
{
if (i == j)
{
dp[i][j] = num[i];
if (dp[i][j] > maxNum)
{
maxNum = dp[i][j];
ibegin = i;
iend = j;
}
}
else
{
dp[i][j] = dp[i][j - 1] + num[j];
if (dp[i][j] > maxNum)
{
maxNum = dp[i][j];
ibegin = i;
iend = j;
}
}
}

cout << maxNum << endl;//输出最大和
for (int i = ibegin; i <= iend; i++)//输出最大和对应的子序列
cout << num[i] << ' ';
cout << endl;
}

int main()
{
//处理输入
int n;
cin >> n;
int i = n;
vector <int> num;
while (i--)
{
int tmp;
cin >> tmp;
num.push_back(tmp);
}
//查找最大和子序列
findTheMaxSumSeq(num);
return 0;
}
``````