hdu5662 YJQQQAQ and the function (单调栈)
来源:互联网 发布:ubuntu terminator 编辑:程序博客网 时间:2024/05/20 00:12
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 l≤r and r×k≤n , 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(1≤T≤3) ——The number of the test cases. For each test case:
The first line contains an integersn(1≤n≤300,000) .
The second line containsn integers describing the given array A , the i th integer is Ai(1≤Ai≤1,000,000) . Between each two adjacent integers there is a white space separated.
The first line contains an integers
The second line contains
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题意:给你n个数,你要找到3个数,l,r,k,l<=r,r*k<=n且p*q*sqrt(k)最小,其中p是A[l*k],A[(l+1)*k]...,A[r*k]的和,q是这些数的最小值。思路:看到求一些数的和乘这些数中最小值的最小值,容易想到单调栈,我们只要先把在k确定的情况下找出所有符合条件的A[],然后再用单调栈找出以每一个数位最小值的左右边界,然后更新ans的最大值就行了。ps:还是不习惯用单调栈,所以用两遍单调队列做了,时间复杂度会差一下,不过差不多。#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<string>#include<bitset>#include<algorithm>using namespace std;#define lth th<<1#define rth th<<1|1typedef long long ll;typedef long double ldb;#define inf 99999999#define pi acos(-1.0)#define maxn 300050int b[maxn],a[maxn];ll sum[maxn];int L[maxn],R[maxn];int q[511111][2];int main(){ int n,m,i,j,T,l,r,k; int front,rear; ll ans; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d",&a[i]); } ans=0; for(k=1;k<=n;k++){ int tot=0; sum[0]=0; for(i=1;i*k<=n;i++){ tot++; b[tot]=a[i*k]; sum[tot]=sum[tot-1]+b[tot]; } front=1,rear=0; for(i=1;i<=tot;i++){ while(front<=rear && q[rear][0]>=b[i] ){ rear--; } if(rear==0){ L[i]=1; } else{ L[i]=q[rear][1]+1; } rear++; q[rear][0]=b[i];q[rear][1]=i; } front=1,rear=0; for(i=tot;i>=1;i--){ while(front<=rear && q[rear][0]>=b[i] ){ rear--; } if(rear==0){ R[i]=tot; } else{ R[i]=q[rear][1]-1; } rear++; q[rear][0]=b[i];q[rear][1]=i; ans=max(ans,(sum[R[i] ]-sum[L[i]-1 ])*b[i]*(int)sqrt((double)k) ); } } printf("%lld\n",ans); } return 0;}
0 0
- hdu5662 YJQQQAQ and the function (单调栈)
- HDU-5662 YJQQQAQ and the function (枚举&&单调栈)
- HDU 5662 YJQQQAQ and the function
- HDU 5662 YJQQQAQ and the function
- the function Passing and returning
- hdu 5875 Function 单调栈 + 暴力
- hdu5875 Function 模运算 + 单调栈
- HDU 5875 Function (单调栈+暴力)
- Mike and Feet(单调栈)
- Passing the Message(单调栈)
- C Function Call Conventions and the Stack
- ocx function and the order of BEGIN_DISPATCH_MAP
- C Function Call Conventions and the Stack
- Codeforces862 E. Mahmoud and Ehab and the function
- [HDU 5875] Function (单调栈/(RMQ+二分))
- HDU 5875 Function(单调栈+在线倍增法)
- hdu4923 Room and Moor 单调栈
- [HDOJ 4923] Room and Moor [单调栈]
- Appro-RTSP详细结构分析——基于live555的视频直播
- IOS生成GUID
- shiro redis集群介绍
- IMS的概念和发展_C114通信百科
- ubuntu下安装svn
- hdu5662 YJQQQAQ and the function (单调栈)
- C++中placement new操作符(经典)
- Oracle Redo Log 机制小结(转载)
- Asp.net mvc 强类型View
- 检测端口是否打开
- Java中,当表单含有文件上传时,提交数据的如何读取(感谢作者分享)
- 【算法】PHP实现经典算法(下)
- android 输入框EditText禁止输入表情符
- Qt之串口编程