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 integers n(1≤n≤300,000).

The second line contains n integers
describing the given array A,
the ith
integer is Ai(1≤Ai≤1,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

1
3
2 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|1
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 300050
int 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; }

hdu5662 YJQQQAQ and the function (单调栈)的更多相关文章

  1. 2016 大连网赛---Function(单调栈)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5875 Problem Description The shorter, the simpl ...

  2. HDU 5875 Function (线段树+gcd / 单调栈)

    题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对数字b取模时:如果a<b,则等于原数,否则a会变小至少一半. ...

  3. HDU 5875 H - Function 用单调栈水过了

    http://acm.hdu.edu.cn/showproblem.php?pid=5875 单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以. 这题是数据水而已. 这里学习下单调栈 ...

  4. Function:凸包,单调栈,题意转化,单峰函数三分,离线处理

    很难啊啊啊!!! bzoj5380原题,应该可以粘题面. 问题转换: 有一个n列1e9行的矩阵,每一列上都写着相同的数字Ai. 你从位置(x,y)出发每一步可以向左上方或左方走一步,最后走到第一行. ...

  5. poj2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  6. Codeforces #123D: 后缀数组+单调栈

    D. String     You are given a string s. Each pair of numbers l and r that fulfill the condition 1 ≤  ...

  7. hdu 5875(单调栈)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  8. 【BZOJ3238】差异(后缀数组,单调栈)

    题意: 思路:显然len(t[i])+len(t[j])这部分的和是一定的 那么问题就在于如何快速求出两两之间lcp之和 考虑将它们排名后用SA可以很方便的求出lcp,且对答案没有影响,因为形式都是数 ...

  9. php实现包含min函数的栈(这个题目用另外一个栈做单调栈的话时间复杂度会低很多)

    php实现包含min函数的栈(这个题目用另外一个栈做单调栈的话时间复杂度会低很多) 一.总结 这个题目用另外一个栈做单调栈的话时间复杂度会低很多 二.php实现包含min函数的栈 题目描述 定义栈的数 ...

随机推荐

  1. java进阶(29)--HashMap集合

    一.HashMap简介 1.HashMap底层是哈希表结构,类似字典,初始化如下: 2.哈希表结构: 是一个数组+单向链表的结构体 数组:查询效率较高,随机增删效率很低 单向链表:在随机增删方面效率较 ...

  2. js 判断用户是手机端还是电脑端访问

    通过userAgent 判断,网页可以直接使用 navigation对象 node端 可以通过请求头的 ctx.request.header['user-agent'] const browser = ...

  3. SQL注入-流程

    一般注入分类: 时间,布尔,报错,堆,联合 有关函数介绍: current_user() 当前用户名 session_user() 链接数据库的用户名 @@basedir mysql安装路径 @@da ...

  4. Head First 设计模式 —— 14. 复合 (Compound) 模式

    复合模式 在一个解决方案中结合两个或多个模式,以解决一般或重复发生的问题. P500 思考题 public interface Quackable { public void quack(); } p ...

  5. DHCP最佳实践(三)

    这是Windows DHCP最佳实践和技巧的最终指南. 如果您有任何最佳做法或技巧,请在下面的评论中发布它们. 在本指南(三)中,我将分享以下DHCP最佳实践和技巧. 仅在需要时才使用IP冲突检测 运 ...

  6. P1140 相似基因(字符串距离,递推)

    题目链接: https://www.luogu.org/problemnew/show/P1140 题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了44种核苷酸,简记作A,C,G,TA,C, ...

  7. leetcode刷题录-1395

    目录 题目 思考过程 查看别人分享的思路 总结 题目 题目地址:https://leetcode-cn.com/problems/count-number-of-teams/ n 名士兵站成一排.每个 ...

  8. oracle_fdw的安装和使用

    1.下载instant oracle client 下载网址:https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html ...

  9. 【一天一个知识点系列】- Redis Cluser之数据分布

    数据分布 简述 分布式数据库首先要解决把整个数据集按照分区规则映射到多个节点的问题,即把数据集划分到多个节点上,每个节点负责整体数据的一个子集 分区及限制 分区规则 常见的分区规则 顺序分区 哈希分区 ...

  10. qt for webassembly环境搭建图文教程

    一.前言 从Qt5.14开始,官方的在线安装提供了qt for webassembly构建套件,这对很多小白来说绝对是个好消息,也绝对是个好东西,好消息是不用再去交叉编译自己生成qt for weba ...