1553: Good subsequence

Submit Page    Summary    Time Limit: 2 Sec     Memory Limit: 256 Mb     Submitted: 895     Solved: 335


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
问你符合要求的子段的最大长度是多少?
符合要求的子段:子段的最大值和最小值的差小于等于k
分析:
利用set模拟该过程,或者直接暴力
不知道怎么描述这个过程
不过按照代码模拟一下样例1应该就明白了
 
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define max_v 10005
using namespace std;
int a[max_v];
set<int> s;
int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
s.clear();
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
int j=;
int ans=;
for(int i=;i<=n;i++)
{
s.insert(a[i]);
if(*s.rbegin()-*s.begin()>k)
{
s.erase(s.find(a[j]));
j++;
}
ans=max(ans,i-j+);
}
printf("%d\n",ans);
}
return ;
}
/*
给你一个序列,长度为n
然后给一个数字k
问你符合要求的子段的最大长度是多少?
符合要求的子段:子段的最大值和最小值的差小于等于k 分析:
利用set模拟该过程,或者直接暴力
不知道怎么描述这个过程
不过按照代码模拟一下样例1应该就明白了
*/

1553: Good subsequence (很奇妙的set模拟题,也可以直接暴力)的更多相关文章

  1. hdu_1034(模拟题)

    很久没有打模拟题了,再次总结一下模拟题的做法: 仔细分析题意,弄清楚过程 理清楚模拟步骤,严格按照步骤编写代码 添加中间输出测试每步结果 虽然这是一个很简单的水题,但是没有松哥帮忙还是卡了很久,因为没 ...

  2. 对于springmvc 很奇妙的报404错误的记录

    @RequestMapping("/editItems") public ModelAndView editItems(Integer id) throws Exception { ...

  3. 全国信息学奥林匹克联赛 ( NOIP2014) 复赛 模拟题 Day1 长乐一中

    题目名称 正确答案  序列问题 长途旅行 英文名称 answer sequence travel 输入文件名 answer.in sequence.in travel.in 输出文件名 answer. ...

  4. csu 1312 榜单(模拟题)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1312 1312: 榜单 Time Limit: 1 Sec  Memory Limit: 128 ...

  5. ZOJ1111:Poker Hands(模拟题)

    A poker deck contains 52 cards - each card has a suit which is one of clubs, diamonds, hearts, or sp ...

  6. HDU 4452 Running Rabbits (模拟题)

    题意: 有两只兔子,一只在左上角,一只在右上角,两只兔子有自己的移动速度(每小时),和初始移动方向. 现在有3种可能让他们转向:撞墙:移动过程中撞墙,掉头走未完成的路. 相碰: 两只兔子在K点整(即处 ...

  7. 《ACM国际大学生程序设计竞赛题解Ⅰ》——模拟题

    这篇文章来介绍一些模拟题,即一类按照题目要求将现实的操作转换成程序语言. zoj1003: On every June 1st, the Children's Day, there will be a ...

  8. POJ 模拟题集合

    http://www.cppblog.com/Uriel/articles/101592.html 感觉这个暑假没有去年有激情啊,,,还没到状态就已经块上学了,,, 真是弱暴了,,,找几道模拟题刷刷. ...

  9. Heap Operations(模拟题)

     Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. 【代码笔记】iOS-ios7 StatusBar

    代码: RootViewController.m #import "RootViewController.h" @interface RootViewController () @ ...

  2. vs2017 + Python3.6 +Django1.11 连接mysql数据库

    不废话直接来. vs2017创建一个新的python web项目之后默认链接数据库是sqlite.但是我就想连接到Mysql 上面玩,于是开始倒腾了.下面是步骤 1.修改settings.py 文件需 ...

  3. h5新属性

    <!DOCTYPE html> <html lang="en"> <head> <meta id="viewport" ...

  4. Atitit.resin  could not create the java virtual machine问题

    Atitit.resin  could not create the java virtual machine问题 1. 正常的参数是这样1 2. 错误的cmd运行时候的参数1 3. 输出2 4. 原 ...

  5. Java集合 -- HashSet 和 HashMap

    HashSet 集合 HashMap 集合 HashSet集合 1.1 Set 接口的特点 Set体系的集合: A:存入集合的顺序和取出集合的顺序不一致 B:没有索引 C:存入集合的元素没有重复 1. ...

  6. Android批量打包提速 - 1分钟900个市场不是梦

    版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/4152323.html 黎明前的黑暗 使用Ant或者Gradl ...

  7. sql 单表查询练习

    -- 工资高于3000的员工select * from emp where sal > 3000;-- 工资在2500和3000之间的员工select * from emp where sal ...

  8. 【Python】Java程序员学习Python(九)— 集合(list、tuple、range)和映射(dict)

    集合是一门语言最重要的一个特性,对集合支持的程度越好,使用起来越方便 一.表现形式 1.1 list list的基本用法: 使用中括号包裹,[1,2,3] 元素类型可以是任意类型,同样可以是list ...

  9. 回归JavaScript基础(七)

    主题:引用类型Function的介绍. 今天首先说的就是Function类型.下面就是定义函数的两种方法,第一种使用函数声明语法定义,第二种使用函数表达式定义.这两种定义函数的方式几乎没有什么区别. ...

  10. 在 Azure 中的 Linux VM 上创建 MongoDB、Express、AngularJS 和 Node.js (MEAN) 堆栈

    本教程介绍如何在 Azure 中的 Linux VM 上实现 MongoDB.Express.AngularJS 和 Node.js (MEAN) 堆栈. 通过创建的 MEAN 堆栈,可以在数据库中添 ...