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. %lld 和 %I64d的区别

    参考一个博客的链接:https://blog.csdn.net/thunders01/article/details/38879553

  2. Windows常用的CMD命令

    mspaint 打开画图 write 打开写字板 explorer 打开文件资源管理器 notepad 打开记事本 devmgmt.msc 打开设备管理器 regedit 打开注册表编辑器 Mscon ...

  3. 浅谈IIS 和 asp.net的应用之间的关系

    IIS可以理解为一个web服务器. 用于提供web相关的各种服务. IIS6.0中添加了一个新的功能, application pool. application pool的作用是将运行在同一个ser ...

  4. Leetcode_Easy_14

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". class Solution { public String longestCommonP ...

  5. linux 进阶命令笔记(12月26日)

    1. df 指令 作用:查看磁盘空间 用法: #df -h       -h 表示以可读性较高的形式展示大小   2.free 指令 作用:查看内存使用情况 语法:#free -m       -m表 ...

  6. Unity--game

    打怪兽--头像状态 Git :https://github.com/vinieo/attck 打怪兽--背景音乐音量 Git :https://github.com/vinieo/ack_bgm 小球 ...

  7. Mongodb 创建管理员帐号与普通帐号

    数据库操作权限 readAnyDatabase 任何数据库的只读权限 userAdminAnyDatabase 任何数据库的读写权限 userAdminAnyDatabase 任何数据库用户的管理权限 ...

  8. 详细解说Tomcat 设置虚拟路径的几种方法及为什么设置虚拟路径

    说明:此次使用的是Tomcat 7.0 很多朋友都会很疑惑,既然我们都知道在Tomcat服务器上部署项目只要将项目打包,然后放到webapps目录下就可以了,为什么还需要配置虚拟路径?的确,把项目放到 ...

  9. 设计模式(三)Singleton Pattern单例设计模式

    1.饿汉式 public class SingletonDemo { private static SingletonDemo s=new SingletonDemo(); private Singl ...

  10. 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句

    在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句  突然看到这个问题,脑袋一蒙,不知道啥意思,后来想想,试图把select里的选项放到后面,问题自然解决!   下面这 ...