HDU 5662 YJQQQAQ and the function

来源:互联网 发布:淘宝同款图片怎么处理 编辑:程序博客网 时间:2024/06/11 15:47
Problem Description
YJQQQAQ has an array A of length n. He defines a function fl,r,k where l,r,k are positive integers that satisfies lr and r×kn, and the value of the function equals to p×q×k where p equals to the sum value of Al×k,A(l+1)×k,...,Ar×k and q equals to the minimal value of them. YJQQQAQ wants to choose the positive integers l,r,k carefully to maximize the value of the function.
 

Input
The first line contains an integer T(1T3)——The number of the test cases. For each test case:
The first line contains an integers n(1n300,000).
The second line contains n integers describing the given array A, the ith integer is Ai(1Ai1,000,000). Between each two adjacent integers there is a white space separated.
 

Output
For each test case, the only line contains the only integer that is the maximum value of the function.
 

Sample Input
132 3 1
 

Sample Output
10
Hint
When and only when $l=1,r=2,k=1$, the value of the function is the maximum.
原来有种东西叫做单调栈
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<map>#include<set>#include<cmath>#include<queue>#include<stack>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<algorithm>#include<functional>using namespace std;typedef __int64 LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int maxn = 3e5 + 10;int T, n, m, a[maxn], b[maxn], l[maxn], r[maxn];LL sum[maxn];int main() {scanf("%d", &T);while (T--){scanf("%d", &n);for (int i = 1; i <= n; i++) scanf("%d", &a[i]);LL ans = 0;for (int i = 1; i <= n; i++){sum[0] = m = 0;for (int j = 1; j*i <= n; j++){b[++m] = a[j*i];sum[m] = sum[m - 1] + b[m];}stack<int> p;for (int j = 1; j <= m; j++){while (!p.empty() && b[p.top()] > b[j]) r[p.top()] = j - 1, p.pop();p.push(j);}while (!p.empty()) r[p.top()] = m, p.pop();for (int j = m; j; j--){while (!p.empty() && b[p.top()] > b[j]) l[p.top()] = j + 1, p.pop();p.push(j);}while (!p.empty()) r[p.top()] = 1, p.pop();for (int j = 1; j <= m; j++) ans = max(ans, (sum[r[j]] - sum[l[j] - 1])*b[j] * (int)sqrt(i));}printf("%I64d\n", ans);}return 0;}


0 0