嘟嘟嘟hdu
嘟嘟嘟vjudge
我连这么简单的分治题都不会呀……菜死了
一不小心就把算法说出来了。
对于一段区间([L, R]),我们要求出这个区间中所有的长度的子区间的答案。
解法很暴力:先(O(n))找出最小值所在位置(pos),然后在([L, pos])中枚举一遍最大值去更新(temp[pos - i + 1])。([pos, R])同理。
之所以是临时的(temp)数组,是因为大区间的答案可能从小区间更新而来。所以区间长度从小到大枚举再更新一遍即可。
然后递归分治下去([L, pos - 1])和([pos + 1, R])。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, a[maxn];
ll ans[maxn], tp[maxn];
void solve(int L, int R)
{
if(L > R) return;
int pos = L, len = R - L + 1;
for(int i = L; i <= R; ++i) if(a[i] < a[pos]) pos = i;
for(int i = 1; i <= len; ++i) tp[i] = 0;
for(int i = L; i <= pos; ++i)
tp[pos - L + 1] = max(tp[pos - L + 1], (ll)a[i] * a[pos]);
for(int i = pos; i <= R; ++i)
tp[R - pos + 1] = max(tp[R - pos + 1], (ll)a[i] * a[pos]);
ll Max = 0;
for(int i = 1; i <= len; ++i)
{
Max = max(Max, tp[i]);
ans[i] = max(ans[i], Max);
}
solve(L, pos - 1); solve(pos + 1, R);
}
int main()
{
while(scanf("%d", &n) != EOF)
{
Mem(ans, 0);
for(int i = 1; i <= n; ++i) a[i] = read();
solve(1, n);
for(int i = 1; i <= n; ++i) write(ans[i]), enter;
}
return 0;
}