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. centos 安装 部署 gitlab github

    https://www.cnblogs.com/wenwei-blog/p/5861450.html 我这里使用的是centos 7 64bit,我试过centos 6也是可以的! 1. 安装依赖软件 ...

  2. Dubbo+Zookeeper(二)Dubbo架构

    上次更新博客已经是一年前,这一年发生了很多事,并不顺利,甚至有些痛苦,不过不管怎样,不要停止学习,只有学习才能让你变强,应对更多不安定. 一.RPC概念 Dubbo服务是一个RPC框架,那我们首先就要 ...

  3. 实验一-最小生成树Kruskal算法

    实验名称 最小生成树算法-Kruskal算法 实验目的 1.掌握并查集的合并优化和查询优化: 2.掌握Kruskal算法. 3.能够针对实际问题,能够正确选择贪心策略. 4.能够针对选择的贪心策略,证 ...

  4. 在Windows中安装MongoDB--图文并茂

    在Windows环境下安装MongoDB的方法 (1)下载MongoDB Windows版: 进入MongoDB官网 (2)设置数据文件和日志文件的存放目录: 打开刚刚安装MongoDB的目录咋bin ...

  5. Python 日志打印之logging.getLogger源码分析

    日志打印之logging.getLogger源码分析 By:授客 QQ:1033553122 #实践环境 WIN 10 Python 3.6.5 #函数说明 logging.getLogger(nam ...

  6. 彻底搞懂MySQL为什么要使用B+树索引

    目录 MySQL的存储结构 表存储结构 B+树索引结构 B+树页节点结构 为什么要用B+树索引 二叉树 多叉树 B树 B+树 搞懂这个问题之前,我们首先来看一下,MySQL表的存储结构 MySQL的存 ...

  7. os-hackos-3-docker提权

    0x00 cewl http://192.168.43.179/websec/爬取页面所有的单词做成字典 hydra -l contact@hacknos.com -P cewl.txt 192.16 ...

  8. paramiko模块简单用法

    最简单最基本的用法 1 #__*__coding:utf-8__*__ 2 import paramiko 3 hostname = '192.168.1.1' 4 username = 'root' ...

  9. 爬虫学习(三)Chrome浏览器使用

    一.新建隐身窗口 在打开隐身窗口的时候,第一次请求某个网站是没有携带cookie的,和代码请求一个网站一样,不携带cookie.这样就能够尽可能的理解代码请求某个网站的结果:除非数据是通过js加载出来 ...

  10. 基于 WebRTC 实现自定义编码分辨率发送

    2020年如果问什么技术领域最火?毫无疑问:音视频.2020年远程办公和在线教育的强势发展,都离不开音视频的身影,视频会议.在线教学.娱乐直播等都是音视频的典型应用场景. 更加丰富的使用场景更需要我们 ...