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. 消息队列之ActiveMQ简单环境搭建

    准备: 环境:win7,Eclipse,jdk1.8 ActiveMQ版本:ActiveMQ 5.9.0 Release下载地址:http://activemq.apache.org/download ...

  2. hihoCoder 1116 计算(线段树)

    http://hihocoder.com/problemset/problem/1116 题意: 思路: 用线段树解决,每个节点需要设置4个变量,sum记录答案,all记录整个区间的乘积,pre记录该 ...

  3. data:image/png;base64 上传图像将图片转换成base64格式

    大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJ ...

  4. 小程序之image图片实现宽度100%,高度自适应

    哇 今天搞了半天  图片一直变形啊啊啊啊 宽度100%   高度给100%   给auto   完全不管用啊啊啊啊 然后最后最终!!!! 首先我们要给我们的图片一个100%的宽度!让它自适应!! .g ...

  5. 转 class和struct最本质的区别

    class和struct最本质的区别是class是引用类型,而struct是值类型,它们在内存中的分配情况有所区别. 什么是class? class(类)是面向对象编程的基本概念,是一种自定义数据结构 ...

  6. http_load 高并发测试

    安装http_load 下载 sudo wget http://www.acme.com/software/http_load/http_load-09Mar2016.tar.gz 解压 sudo t ...

  7. leecode第一百四十一题(环形链表)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  8. 用友u8采购发票如何取消审核

    流程是应付系统---应付单据审核---过滤---选择日期+已审---选择相应发票---弃审

  9. HRBUST - 2358 Magic network

    HRBUST - 2358 思路:dfs序 + 树状数组 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimiz ...

  10. 非递归遍历二叉树Java实现

    2018-10-03 20:16:53 非递归遍历二叉树是使用堆栈来进行保存,个人推荐使用双while结构,完全按照遍历顺序来进行堆栈的操作,当然在前序和后序的遍历过程中还有其他的压栈流程. 一.Bi ...