题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553

Description

Give you a sequence of n numbers, and a number k you should find the max length of Good subsequence. Good subsequence is a continuous subsequence of the given sequence and its maximum value - minimum value<=k. For example n=5, k=2, the sequence ={5, 4, 2, 3, 1}. The answer is 3, the good subsequence are {4, 2, 3} or {2, 3, 1}.

Input

There are several test cases.
Each test case contains two line. the first line are two numbers indicates n and k (1<=n<=10,000, 1<=k<=1,000,000,000). The second line give the sequence of n numbers a[i] (1<=i<=n, 1<=a[i]<=1,000,000,000). 
The input will finish with the end of file.

Output

For each the case, output one integer indicates the answer.

Sample Input

5 2
5 4 2 3 1
1 1
1

Sample Output

3
1

Hint

Source

题意:

  给你一个n个数,在这些数里面找一段连续区间,这个区间需要满足这个区间里的最大值-最小值 <= k。找出最大的区间长度。

题解:

  拿到题的时候,就想到RMQ来找区间最值,二分长度找最大值。

  然后队伍分工合作,一个人写二分,我写RMQ。一不小心写错了一个点wa了一次,QAQ。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
#define LNF (1<<60)
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = 1e9+; LL st_max[][maxn], st_min[][maxn];
LL A[maxn];
void RMQ_init(int n){
for(int i=;i<n;i++) st_max[][i]=st_min[][i]= A[i];
for(int j = ;(<<j) <= n;j++){
int k = <<(j-);
for(int i=;i+k<n;i++){
st_max[j][i] = max(st_max[j-][i], st_max[j-][i+k]);
st_min[j][i] = min(st_min[j-][i], st_min[j-][i+k]);
}
}
}
LL RMQ(int a, int b){
int dis = abs(b-a) + ;
int k;
for(k=;(<<k)<=dis;k++);
k--;
// printf("%lld %lld\n", max(st_max[k][a], st_max[k][b-(1<<k)+1]), min(st_min[k][a], st_min[k][b-(1<<k)+1]));
return max(st_max[k][a], st_max[k][b-(<<k)+])-min(st_min[k][a], st_min[k][b-(<<k)+]);
}
int test(int len, int n, int k)
{
for(int i = len-; i<n; i++){
if(RMQ(i-len+, i)<=1LL*k)
return ;
}
return ;
}
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL int n, k;
while(~scanf("%d%d", &n, &k)){
ms(st_max, );
ms(st_min, );
ms(A, );
for(int i=;i<n;i++) scanf("%lld", &A[i]);
RMQ_init(n);
// printf("%lld\n", RMQ(0, n-1));
int l = , r = n+;
int mid, ans;
while(l<r){
mid = (l+r)/;
if(test(mid, n, k)){
ans = mid;
l = mid+;
}
else
r = mid;
}
printf("%d\n", ans);
}
return ;
}

模板题。XD

最新文章

  1. UIView--震动效果
  2. javascript 取整,取余数
  3. 解决方案: scp/ssh 的登陆提示很慢 (Linux)
  4. [转]highcharts图表入门之:如何让highcharts图表自适应浏览器窗体的大小或者页面大小
  5. ubuntu14.04配置中文latex完美环境(texlive+texmaker+lyx)
  6. Topcoder SRM 598 div2
  7. JDBC学习笔记(6)——获取自动生成的主键值&amp;处理Blob&amp;数据库事务处理
  8. Linux如何查找文件安装路径?
  9. NOIP2014-普及组复赛-第四题-子矩阵
  10. js函数——setinterval和setTimeout
  11. [转载] gitbook安装与使用
  12. ASP.NET MVC5高级编程 之 HTML辅助方法
  13. 笔记:Activity的启动过程
  14. Ubuntu 中卸载软件的几种命令
  15. 转:桩模块 stub 和驱动模块 driver
  16. 【深入理解javascript】闭包
  17. git忽略操作
  18. abp+angular+bootstrap-table的使用
  19. 常用SQL语句大全(SQL Server)
  20. 用cookies判断用户首次登录

热门文章

  1. Python文件读写基本操作
  2. jdbc步骤:
  3. 工具 - MSF
  4. 存储过程SET XACT_ABORT ON
  5. javascript数组排序和prototype详解
  6. JDK11 | 第五篇 : 启动单个Java源代码文件的程序
  7. 搜索(BFS)---最短单词路径
  8. Linux压缩、解压
  9. vue.js(12)--过滤器
  10. 行人重识别(ReID) ——数据集描述 CUHK03