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 ...
随机推荐
- php 自己封装的一些函数
手机归属地函数 function get_mobile_area($phone){ $sms = array('province'=>'', 'supplier'=>''); //初始化变 ...
- CSS布局技巧大全
参考资料: http://www.imooc.com/article/2235 单列布局 水平居中 父元素text-align:center;子元素:inline-block; 优点:兼容性好: 不足 ...
- makefile初步制作,arm-linux- (gcc/ld/objcopy/objdump)详解
在linux中输入vi Makefile 来实现创建Makefile文件 注意:命令行前必须加TAB键 例如:将两个文件led.c和crt0.S汇编文件,制作一个Makefile文件 led.bin ...
- macOS上的ODBC-利用unixODBC连接PostgreSQL与SQLite并进行数据迁移
安装UnixODBC & PSQLODBC driver for UnixODBC $ brew install psqlodbc Updating Homebrew... ==> In ...
- Beta阶段事后诸葛亮分析
1.总结的提纲内容 a. 项目管理之事后诸葛亮会 设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件主要解决用户无意识花钱,无法清楚看见钱去 ...
- 团队作业8----第二次项目冲刺(Beta阶段) 第五天
BETA阶段冲刺第五天 1.小会议ing 2.每个人的工作 (1)昨天已完成的工作 文件读取的方式采用按钮的: (2) 今天计划完成的工作 (3) 工作中遇到的困难: 林莹:源代码的部分我们已经初步完 ...
- 201521123083《Java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...
- 201521123108 《Java程序设计》第4周学习总结
1. 本章学习总结 2. 书面作业 Q1. 注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) 答: Q2. 面向对象设计(大作业1-非常重要) 2. ...
- 201521123001《Java程序设计》第2周学习总结
本周学习总结 码云可以很方便地储存我们写好的代码,不用在写代码的时候担心没带U盘 Java中有许多已经写好的具有特定功能的一段独立小程序,不需要每一个函数都自己编写 Java的float型和C语言的不 ...
- 201521123049 《JAVA程序设计》 第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己 ...