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. Docker run 出现问题如何调试?

    docker run -ti 3f5dd697cc83 /bin/bash #进入image的目录 ls -l #列出所有目录 dotnet run WestWin.Ads.Crawler.WebAp ...

  2. insert into table (a,b,c) select

    本文为博主原创,转载请注明出处: 在项目中,需要统计数据,从基础表中的数据进行统计,并插入到汇总 表中, (1)语句形式为:Insert into Table2(field1,field2,...) ...

  3. 解决 dpkg: warning: files list file for package 'x' missing 问题

    参考: dpkg: warning: files list file for package 'x' missing 解决 dpkg: warning: files list file for pac ...

  4. jenkins 异常

    八月 03, 2017 7:23:54 下午 org.eclipse.jetty.util.log.JavaUtilLog warn 警告: FAILED ServerConnector@29e6eb ...

  5. Windows has encountered a critical problem and will restart automatically in one minute. Please save your work now

    Windows has encountered a critical problem and will restart automatically in one minute. Please save ...

  6. 使用R语言的RTCGA包获取TCGA数据--转载

    转载生信技能树 https://mp.weixin.qq.com/s/JB_329LCWqo5dY6MLawfEA TCGA数据源 - R包RTCGA的简单介绍 - 首先安装及加载包 - 指定任意基因 ...

  7. SHA-256 加密原理

    网络中传输敏感信息的时候通常会对字符串做加密解密处理 SHA-256 加密原理

  8. 【NPOI】WebAPI-使用NPOI导出Excel

    1.安装nuget包 2.封装方法 public byte[] ExportToByteArray(IEnumerable<string> headerText, IEnumerable& ...

  9. PHP直接将文件流转换为字符串

    有时候不需要图片直接输出到浏览器,需要如下处理! 输出到浏览器 $qrCode = new QrCode(); $qrCode ->setText('Life is too short to b ...

  10. C/C++.文件是否存在

    1. 2._access, _waccess.html(https://msdn.microsoft.com/en-us/library/1w06ktdy.aspx) int _access( con ...