Sequence query 好题啊
Sequence query
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 好题啊的更多相关文章
- hdu 1005:Number Sequence(水题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- BZOJ1345 Baltic2007 序列问题Sequence 【思维题】*
BZOJ1345 Baltic2007 序列问题Sequence Description 对于一个给定的序列a1,…,an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用 ...
- HDU——1005Number Sequence(模版题 二维矩阵快速幂+操作符重载)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdoj--1005--Number Sequence(规律题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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 ...
- 5805 NanoApe Loves Sequence(想法题)
传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K ( ...
- 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 ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
随机推荐
- adb 安装apk 报错:Failure [INSTALL_FAILED_CPU_ABI_INCOMPATIBLE]
这是因为系统里缺少了 Google Play 市场等各种谷歌服务应用,其实是因为版权问题,从 2.0 版本开始 Genymotion 提供的虚拟设备都已经移除了 Google Apps 以及 AR ...
- C++三种野指针及应对/内存泄露
野指针,也就是指向不可用内存区域的指针.如果对野指针进行操作,将会使程序发生不可预知的错误,甚至可能直接引起崩溃. 野指针不是NULL指针,是指向"垃圾"内存的指 ...
- 关于本地代码挂载到vm虚拟环境下运行
第一步: 首先你得装个 VM 虚拟机 然后新建一个Linux虚拟环境(建议CentOS镜像)(PS:至于安装此处就省略.....) 第二步:启动虚拟机配置 lnmp (这里我们可以使用 lnmp的 ...
- TX2017秋招笔试题之编码
问题描述: 假定一种编码的编码范围是a ~ y的25个字母,从1位到4位的编码,如果我们把该编码按字典序排序,形成一个数组如下: a, aa, aaa, aaaa, aaab, aaac, - -, ...
- 探索 Java 热部署
在 JAVA 开发领域,热部署一直是一个难以解决的问题,目前的 JAVA 虚拟机只能实现方法体的修改热部署,对于整个类的结构修改,仍然需要重启虚拟机,对类重新加载才能完成更新操作.对于某些大型的应用来 ...
- JS的this总结(上)-call()和apply()
JS的this总结(上)-call()和apply() 相信很多人在学习JavaScript的过程中,都会了解到this,而大部分人都会特意去网络上搜一下相关资料,大部分的文章都有这么一句话: t ...
- 后台方庄List razor 循环
后台: //1.查询所有年卡类型 StringBuilder sqlStr = new StringBuilder(); sqlStr.Ap ...
- 【集美大学1411_助教博客】团队作业4——第一次项目冲刺(Alpha版本) 成绩
写在前面的话 第一次alpha冲刺,大家完成得都很好,从博客中体现出来的是所有团队都完成了预期的目标. 一个团队从最初建立,到磨合,到做出一个能被接受的产品,真是一件不容易的事情.就好像TheNexu ...
- 201521123087 《java程序设计》 第七周学习总结
1. 本周学习总结 2. 书面作业 ArrayList代码分析1.1 解释ArrayList的contains源代码 ...
- 201521123040《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过? ...