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.  
1
3
2 3 1
 

Sample Output
  1.  
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:还是不习惯用单调栈,所以用两遍单调队列做了,时间复杂度会差一下,不过差不多。
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<math.h>
  6. #include<vector>
  7. #include<map>
  8. #include<set>
  9. #include<string>
  10. #include<bitset>
  11. #include<algorithm>
  12. using namespace std;
  13. #define lth th<<1
  14. #define rth th<<1|1
  15. typedef long long ll;
  16. typedef long double ldb;
  17. #define inf 99999999
  18. #define pi acos(-1.0)
  19. #define maxn 300050
  20. int b[maxn],a[maxn];
  21. ll sum[maxn];
  22. int L[maxn],R[maxn];
  23. int q[511111][2];
  24. int main()
  25. {
  26. int n,m,i,j,T,l,r,k;
  27. int front,rear;
  28. ll ans;
  29. scanf("%d",&T);
  30. while(T--)
  31. {
  32. scanf("%d",&n);
  33. for(i=1;i<=n;i++){
  34. scanf("%d",&a[i]);
  35. }
  36. ans=0;
  37. for(k=1;k<=n;k++){
  38. int tot=0;
  39. sum[0]=0;
  40. for(i=1;i*k<=n;i++){
  41. tot++;
  42. b[tot]=a[i*k];
  43. sum[tot]=sum[tot-1]+b[tot];
  44. }
  45. front=1,rear=0;
  46. for(i=1;i<=tot;i++){
  47. while(front<=rear && q[rear][0]>=b[i] ){
  48. rear--;
  49. }
  50. if(rear==0){
  51. L[i]=1;
  52. }
  53. else{
  54. L[i]=q[rear][1]+1;
  55. }
  56. rear++;
  57. q[rear][0]=b[i];q[rear][1]=i;
  58. }
  59. front=1,rear=0;
  60. for(i=tot;i>=1;i--){
  61. while(front<=rear && q[rear][0]>=b[i] ){
  62. rear--;
  63. }
  64. if(rear==0){
  65. R[i]=tot;
  66. }
  67. else{
  68. R[i]=q[rear][1]-1;
  69. }
  70. rear++;
  71. q[rear][0]=b[i];q[rear][1]=i;
  72. ans=max(ans,(sum[R[i] ]-sum[L[i]-1 ])*b[i]*(int)sqrt((double)k) );
  73. }
  74. }
  75. printf("%lld\n",ans);
  76. }
  77. return 0;
  78. }

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. SonarQube学习(五)- SonarQube之自定义规则使用

    一.前言 古人云:"欲速则不达",最近真的是深有体会.学习也是如此,不是一件着急的事,越是着急越不会. 就拿SonarQube来说吧,去年年末就想学来着,但是想着想着就搁置了,有时 ...

  2. 【C++】《C++ Primer 》第十六章

    第十六章 模板与泛型编程 面向对象编程和泛型编程都能处理在编写程序时不知道类型的情况. OOP能处理类型在程序允许之前都未知的情况. 泛型编程在编译时就可以获知类型. 一.定义模板 模板:模板是泛型编 ...

  3. Deep Learn I'm back.

    Intorduction: 时隔好几个月,我准备重新进入Deep Learning 的领域.昨天和老师聊了很多,之前觉得我做的工作就是排列组合,在水论文,灌水.但老师却说:这也是为将来的研究打基础. ...

  4. Docker 镜像基础(三)

    基于Dockerfile制作yum版本nginx镜像 [root@node-2 ~]# mkdir /opt/nginx [root@node-2 ~]# cd /opt/nginx/ ## 创建Do ...

  5. scp传文件夹

    scp -r /root/backupdb/2014-08-15(文件夹)    root@192.168.1.98:/root(目录)

  6. 【EXP/IMP】问题总结

    为了使测试与生产数据保持一致,只需要导出数据的时候,可以将测试库的表truncate,保留其它如索引,trigger,constraints,grants等不用再重新导. exp时候rows=y,其它 ...

  7. MongoDB查询优化--explain,慢日志

    引入 与Mysql数据库一样,MongoDB也有自己的查询优化工具,explain和慢日志 explain shell命令格式 db.collection.explain().<method(. ...

  8. RocketMq消息 demo

    参考 https://blog.csdn.net/asdf08442a/article/details/54882769 整理出来的测试 demo 1.produce 生产者 1 package co ...

  9. ElasticSearch Python 基本操作

    创建索引 from elasticsearch import Elasticsearch es = Elasticsearch('192.168.149.96:9200') mappings = { ...

  10. Spider爬虫基础

    get获取某个网站的html代码,post访问网站获取网站返回的信息 import urllib.request import urllib.parse #使用get请求 def start1(): ...