Sequence query

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
SubmitStatus

Problem Description

Given a sequence of  N positive numbers,and M queries

A query maybe :

1 x,find the Maximun "weight" of the consecutive subsequence whose length >= x

2 x,  find the Minimum length of the consecutive subsequence whose weight >= x

the weight of a consecutive subsequence Seq:weight(Seq) = length of Seq * minimum number in Seq.

Input

The first line is an integer T(1<=T<=100) ,the number of test cases;

For each test case,

the first line contains two integer N,M(1<=N,M<=100000)

the second line contains N positive integers(all between [1,2^31-1])

The following M lines contains queries as descripted above, all number is within signed ilong long

any subsequences should not be empty(length >= 1)

Output

for each query,output a line contains an answer you find,if the answer doesn't exist,output -1

Sample Input

1
2 2
1 2
1 2
2 1

Sample Output

2
1

用单调栈预处理出当以 a[i]为最小值的时候,最左向左延伸到哪里,最右向右延伸到哪里
不妨设为 lt[i],rt[i].
伪代码: a[0] = a[n + 1] = -1;
for(int i = 1; i <= n; i ++) {
lt[i] = i;
while(a[i] <= a[lt[i] - 1]) lt[i] = lt[lt[i] - 1];
}
rt[i]同样处理。当然,有很多种处理方法。
用 max_len[i]表示,当长度为 i 的时候,延伸长度不小于 i 的 ai 的最大值, for(int i = 1; i <= n; i ++) max_len[rt[i] - lt[i] + 1] = max(max_len[rt[i] - lt[i] + 1],a[i]);
max_len[i] = max(max_len[i],max_len[i + 1])
对于第一问,处理一个后缀,dp[i] = max(dp[i + 1],max_len[i] * i)
输入一个 x,判掉不合法的之后,直接输出 dp[x]
对于第二问,和第一问类似,处理一个前缀,然后需要二分。

 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
using namespace std;
#define ll long long
ll a[],lt[],rt[],n;
ll dp[],dp1[],maxlen[];
void fun(ll x)
{
int l,r,ans,m;
l=,r=n,ans=-;
while(l<=r)
{
m=(l+r)>>;
if(x>dp1[m])
{
l=m+;
}
else
{
r=m-;
ans=m;
}
}
printf("%d\n",ans);
}
int main()
{
//freopen("in.txt","r",stdin);
int t,i,m;
ll x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
a[n+]=a[]=-;
for(i=; i<=n; i++)
scanf("%lld",&a[i]);
for(i=; i<=n; i++)
{
lt[i]=i;
while(a[i]<=a[lt[i]-])lt[i]=lt[lt[i]-];
}
for(i=n; i>=; i--)
{
rt[i]=i;
while(a[i]<=a[rt[i]+])rt[i]=rt[rt[i]+];
}
memset(maxlen,,sizeof(maxlen));
for(i=; i<=n; i++)
maxlen[rt[i]-lt[i]+]=max(maxlen[rt[i]-lt[i]+],a[i]);
for(i=n-; i>=; i--)
maxlen[i]=max(maxlen[i],maxlen[i+]);
memset(dp,,sizeof(dp));
memset(dp1,,sizeof(dp1));
for(i=n; i>=; i--)
dp[i]=max(dp[i+],maxlen[i]*i);
for(i=; i<=n; i++)
dp1[i]=max(dp1[i-],maxlen[i]*i); for(i=; i<m; i++)
{
scanf("%lld%lld",&x,&y);
if(x==)
{
if(y>n)
printf("-1\n");
else
{
if(y<=)
y=;
printf("%lld\n",dp[y]);
} }
else
{
fun(y);
}
}
}
}

Sequence query 好题啊的更多相关文章

  1. hdu 1005:Number Sequence(水题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. BZOJ1345 Baltic2007 序列问题Sequence 【思维题】*

    BZOJ1345 Baltic2007 序列问题Sequence Description 对于一个给定的序列a1,…,an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用 ...

  4. HDU——1005Number Sequence(模版题 二维矩阵快速幂+操作符重载)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. hdoj--1005--Number Sequence(规律题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)

    传送门 Description Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems ...

  7. 5805 NanoApe Loves Sequence(想法题)

    传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K ( ...

  8. HDU 5783 Divide the Sequence (训练题002 B)

    Description Alice has a sequence A, She wants to split A into as much as possible continuous subsequ ...

  9. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

随机推荐

  1. [2016-03-15]rabbitmq notes

    安装 系统:CentOS 6 环境: rabbitmq 依赖的Erlang环境包 wget https://www.rabbitmq.com/releases/erlang/erlang-18.1-1 ...

  2. 『HTMl5』学习日志

    w3g提供在线校验页面:http://validator.w3.org/ 1.文本框获取焦点 <%@ page language="java" import="ja ...

  3. jQuery与js的length属性

    js:length 属性可返回字符串中的字符数目. stringObject.length jQuery:length 属性包含 jQuery 对象中元素的数目. $(selector).length ...

  4. Java集合类库list(1)ArrayList实例

    public class ArrayListTest { public static void main(String[] args) { //创建空的ArrayList列表 ArrayList al ...

  5. 分布式memcached-虚拟节点

    1.通过memcached服务器下的不同端口来达到模拟多台服务器的效果 2.假设现在有三台memcached服务器,本地分别使用11211,11212,11213三个端口来模拟 ①打开端口 ②连接端口 ...

  6. Express异步进化史

    1.导言 在 Javascript 的世界里,异步(由于JavaScript的单线程运行,所以JavaScript中的异步是可以阻塞的)无处不在. Express 是 node 环境中非常流行的Web ...

  7. 用JS制作一个信息管理平台

    首先,介绍一些需要用到的基本知识. [JSON] JSON是数据交互中,最常用的一种数据格式. 由于各种语言的语法都不相同,在传递数据时,可以将自己语言中的数组.对象等转换为JSON字符串. 传递之后 ...

  8. C语言中无符号数和有符号数之间的运算

    C语言中无符号数和有符号数之间的运算 C语言中有符号数和无符号数进行运算(包括逻辑运算和算术运算)默认会将有符号数看成无符号数进行运算,其中算术运算默认返回无符号数,逻辑运算当然是返回0或1了. un ...

  9. 1000以内完全数(完美数)获取实现---基于python

    """题目: 如果一个数恰好等于它的因子之和,则称该数为"完全数" .各个小于它的约数(真约数,列出某数的约数,去掉该数本身,剩下的就是它的真约数)的 ...

  10. 使用SVG基本操作API

    前面的话 本文将详细介绍SVG基本操作API,并使用这些API操作实例效果 基础API 在javascript中,可以使用一些基本的API来对SVG进行操作 [NS地址] 因为SVG定义在其自身的命令 ...