HDU 5696 区间的价值 暴力DFS

Problem Description
我们定义“区间的价值”为一段区间的最大值*最小值。
一个区间左端点在L,右端点在R,那么该区间的长度为(R−L+1)。
现在聪明的杰西想要知道,对于长度为k的区间,最大价值的区间价值是多少。
当然,由于这个问题过于简单。
我们肯定得加强一下。
我们想要知道的是,对于长度为1∼n的区间,最大价值的区间价值分别是多少。
样例解释:
长度为1的最优区间为2−2 答案为6∗6
长度为2的最优区间为4−5 答案为4∗4
长度为3的最优区间为2−4 答案为2∗6
长度为4的最优区间为2−5 答案为2∗6
长度为5的最优区间为1−5 答案为1∗6

Input
多组测试数据
第一行一个数n(1≤n≤100000)。
第二行n个正整数(1≤ai≤109),下标从1开始。
由于某种不可抗力,ai的值将会是1∼109内随机产生的一个数。(除了样例)

Output
输出共n行,第i行表示区间长度为i的区间中最大的区间价值。

Sample Input
5
1 6 2 4 4

Sample Output
36
16
12
12
6

**题意:**中文题意明显,要注意的是给了5s时间 **思路:**直接暴力DFS,枚举所有区间的最大值即可..
/** @Date    : 2016-11-08-23.07
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#define LL long long
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+2000;

LL a[N];
LL maxx[N];

void dfs(int l, int r)
{
if(l >= r)
return ;
LL mi = INF;
LL ma = 0;
int x;
x = -1;
for(int i = l; i <= r; i++)
{
if(mi > a[i])
{
mi = a[i];
x = i;
}
if(ma < a[i])
{
ma = a[i];
}
}
maxx[r - l + 1] = max(maxx[r - l + 1], mi*ma);
dfs(l, x - 1);
dfs(x + 1, r);

}

int main()
{
int n;
while(~scanf("%d", &n))
{
for(int i = 1; i <= n; i++)
scanf("%lld", a + i);
MMF(maxx);
for(int i = 1; i <= n; i++)
{
maxx[1] = max(maxx[1], a[i]*a[i]);
}
dfs(1, n);
for(int i = n - 1; i >= 1; i--)
maxx[i] = max(maxx[i], maxx[i+1]);
for(int i = 1; i <= n; i++)
printf("%lld ", maxx[i]);

}
return 0;
}
原文地址:https://www.cnblogs.com/Yumesenya/p/6079519.html