Taotao Picks Apples

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1446    Accepted Submission(s): 449

Problem Description
There is an apple tree in front of Taotao's house. When autumn comes, n apples on the tree ripen, and Taotao will go to pick these apples.

When Taotao picks apples, Taotao scans these apples from the first one to the last one. If the current apple is the first apple, or it is strictly higher than the previously picked one, then Taotao will pick this apple; otherwise, he will not pick.

Given the heights of these apples h1,h2,⋯,hn, you are required to answer some independent queries. Each query is two integers p,q, which asks the number of apples Taotao would pick, if the height of the p-th apple were q (instead of hp). Can you answer all these queries?

 
Input
The first line of input is a single line of integer T (1≤T≤10), the number of test cases.

Each test case begins with a line of two integers n,m (1≤n,m≤105), denoting the number of apples and the number of queries. It is then followed by a single line of n integers h1,h2,⋯,hn (1≤hi≤109), denoting the heights of the apples. The next m lines give the queries. Each of these m lines contains two integers p (1≤p≤n) and q (1≤q≤109), as described in the problem statement.

 
Output
For each query, display the answer in a single line.
 
Sample Input
1
5 3
1 2 3 4 4
1 5
5 5
2 3
 
Sample Output
1 5 3

 
 
  如果能看出将这个序列分成两半之后利用一些预处理依旧能在log时间内得到答案的话就好办了,比赛的时候想了半天最后发现看错题目了,以为每次求一下LIS。。。这个是贪心的上升并非最优的。
  我们提前做个ST表,用于处理区间最大值下标的查询。dp[0][i]表示从第一个元素到第i个元素的步数,dp[1][i]表示从i贪心的往后走的步数,对于每个<p,q> ,分成[1,p],[p,n] , 找到[1,p]之间的最大值下标j ,和[p,n]中大于这个最大值的下标k, 答案就是  dp[0][j]+dp[1][k]。
  

 #include <iostream>
#include<cmath>
using namespace std;
int N,a[];
int f[][],dp[][];
void init(){
for(int i=;i<=N;++i)f[i][]=i;
for(int k=;(<<k)<=N;++k){
for(int i=;i+(<<k)-<=N;++i){
if(a[f[i][k-]]>=a[f[i+(<<(k-))][k-]]) f[i][k]=f[i][k-];
else f[i][k]=f[i+(<<(k-))][k-];
}
}
}
int query(int L,int R){
if(R<L)return ;
int k=;
while((<<(k+))-<=R-L) k++;
if(a[f[L][k]]>=a[f[R-(<<k)+][k]]) return f[L][k];
else return f[R-(<<k)+][k];
}
int main() {
int t,n,m,i,p,q;
cin>>t;
while(t--){
scanf("%d%d",&n,&m);
for(i=;i<=n;++i)scanf("%d",a+i);
N=n,init();
dp[][]=;
int maxn=;
for(i=;i<=n;++i){
if(a[i]>a[maxn]){
dp[][i]=dp[][maxn]+;
maxn=i;
}
else dp[][i]=-;
} dp[][n]=;
for(i=n-;i>=;--i){
int l=i+,r=n;
while(l<r){
int mid=l+(r-l)/;
if(a[query(l,mid)]>a[i]) r=mid;
else l=mid+;
}
dp[][i]=a[r]>a[i]?dp[][l]+:;
}
while(m--){
scanf("%d%d",&p,&q);
int x=query(,p-),xx=a[x],ans=dp[][x];
if(q>a[x]) xx=q,ans++;
int l=p+,r=n;
while(l<r){
int mid=l+(r-l)/;
if(a[query(p+,mid)]>xx)r=mid;
else l=mid+;
}
if(a[l]>xx) ans+=dp[][l];
cout<<ans<<endl;
}
}
return ;
}
    
 
    

hdu-6406-dp+ST表的更多相关文章

  1. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  2. Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列

    B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...

  3. 【Codeforces Round #466】E. Cashback DP+ST表

    题意 给定$n$个数,将其划分成若干个连续的子序列,求最小价值,数组价值定义为,数组和减去$\lfloor \frac{k}{c} \rfloor$,$k$为数组长度,$c$为给定数 可以列得朴素方程 ...

  4. 刷题总结——Bob's Race(hdu4123 树形dp+st表)

    题目: Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the ro ...

  5. (DP ST表 线段树)51NOD 1174 区间中最大的数

    给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少.   例如: 1 7 6 3 1.i = 1, j = 3,对应的数为7 6 3,最大的数为7. ...

  6. Find the hotel HDU - 3193 (ST表RMQ)

    Summer again! Flynn is ready for another tour around. Since the tour would take three or more days, ...

  7. Interviewe HDU - 3486 (ST表+枚举 )(非二分,看下这个数据、)

    YaoYao has a company and he wants to employ m people recently. Since his company is so famous, there ...

  8. HDU 2861 (DP+打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2861 题目大意:n个位置,m个人,分成k段,统计分法.S(n)=∑nk=0CknFibonacci(k ...

  9. HDU 4123 Bob’s Race 树的直径+ST表

    Bob’s Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=41 ...

  10. HDU 5875 Function(ST表+二分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...

随机推荐

  1. 说明Heap与stack的差别。

    Heap 是堆,Stack 是栈. 栈与堆都是Java用来在Ram中存放数据的地方,与C++不同,Java会自动管理栈与堆,程序员不能直接设置栈与堆. Java的堆是一个运行时的数据区,类的对象从中分 ...

  2. 微服务架构与实践3_api

    场景分析 描述产品服务,基于REST的接口 表述性状态转移(Representational State Transfer,REST) 任务拆分 将整体要做的工作内容划分成小的任务: 统一时间聚焦一个 ...

  3. maven web项目配置log4j,及log4j参数设置

    本文为博主原创,转载须注明转载地址: 1.在maven项目中引入相关的依赖: 需要依赖的jar为: <!-- 配置日志 --> <dependency> <groupId ...

  4. pyqt笔记2 布局管理

    https://zhuanlan.zhihu.com/p/28559136 绝对布局 相关方法setGeometry().move() 箱式布局 QHBoxLayout和QVBoxLayout是基本的 ...

  5. sublime插件开发手记

    原:http://blog.hickwu.com/sublime插件开发手记   标题: sublime插件开发手记 时间: 2014-01-05 14:58:02 正文: 插件基本结构 基本插件实现 ...

  6. scss切页面

    html <div class="data-list"> <div class="data-list-item"> <div cl ...

  7. N的阶乘(10000) 51 nod——1057 (大数)

    像这些大整数加法或者乘法什么的思想都一样,就是截位存取,累积进位,最后逆序输出就可以啦 PS:小生是用10000来存取的,300MS就能A,如果单个存取有点危险,题目时间限制好像是1000ms,大家可 ...

  8. Matlab中的基本数据类型介绍

    Matlab中支持的数据类型包括: 逻辑(logical)字符(char)数值(numeric)元胞数组(cell)结构体(structure)表格(table)函数句柄(function handl ...

  9. VS2010.STL::list的一个bug

    1.ParameterAnswer_Parse(...) 下 FlistParameterOffset.clear(); 出错(list<DWORD>.clear()) (https:// ...

  10. 《剑指offer》第五十二题(两个链表的第一个公共结点)

    // 面试题52:两个链表的第一个公共结点 // 题目:输入两个链表,找出它们的第一个公共结点. #include <iostream> #include "List.h&quo ...