题目链接

http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=1170

题目

Description

​ 在一个由N个整数组成的数列中,最多能找到多少个位置连续的整数且其中的最大值与最小值之差不超过K呢?

Input

​ 输入包含若干组数据。每组数据的第一行有2个正整数,N(1<=N<=\(10^6\)), K(0<=K<=\(10^6\)),其中N、K的含义同上,接下来一行一共有N个32位有符号整数(32-bit signed integer),依次描绘了这个数列中各个整数的值。

Output

​ 对于每组数据,输出一个正整数,表示在这个数列中最多能找到多少个位置连续的整数且其中的最大值与最小值之差不超过K。

Sample Input

4 2
3 1 5 2 3 2
3 1 2

Sample Output

2
3

Hint

​ 由于数据量较大,建议C++选手选用scanf来读取数据。

题解

因为题目要求连续的位置,那么我们直接扫一遍即可,初始\(l=1,r=1\),期间用multiset维护当前序列有多少个数,每次先判断一下multiset里最大数和最小数的差是否大于k,大于的话则则弹出\(a[l]\),不大于的话则加入\(a[r]\),顺便统计一下答案的最大值即可

AC代码

#include<bits/stdc++.h>
#include<set>
#define ll long long
#define maxn 1000050
using namespace std;
ll a[maxn];
inline ll getnum() {
char c; ll ans = 0; ll flag = 1;
while (!isdigit(c = getchar()) && c != '-');
if (c == '-') flag = -1; else ans = c - '0';
while (isdigit(c = getchar())) ans = ans * 10 + c - '0';
return ans * flag;
}
int main() {
ll n, k;
multiset<ll> s;
while (scanf("%lld%lld", &n, &k) != EOF) {
s.clear();
for (int i = 1; i <= n; i++) {
a[i] = getnum();
}
int l = 1, r = 1;
int ans = 0;
s.insert(a[r]);
while (r <= n) {
ll now = *(--s.end()) - *(s.begin());
if (now > k) {
multiset<ll>::iterator it;
it = s.find(a[l]); l++;
s.erase(it);
}
else {
ans = max(r - l + 1, ans);
if (r + 1 <= n) s.insert(a[++r]);
else break;
}
}
printf("%d\n", ans);
}
return 0;
}
/**********************************************************************
Problem: 1170
User: Artoriax
Language: C++
Result: AC
Time:752 ms
Memory:56700 kb
**********************************************************************/

CSU-1170 A Simple Problem的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  2. POJ 3468 A Simple Problem with Integers(线段树/区间更新)

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

  3. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

  4. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  5. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  6. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  7. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

  8. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

  9. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  10. A Simple Problem with Integers

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77964 Acc ...

随机推荐

  1. CPP-基础:类

    1,成员访问属性 一,对于类的实现来说: private:类内部(包括类域范围内)可访问. protect:类内部(包括类域范围内)或 派生类类内部(包括类域范围内)可访问. public: 类内部和 ...

  2. mingw libgcc_s_sjlj-1.dll is missing

    习惯了在linux环境下工作,编译wingdows平台程序采用mingw工具.编译完,运行exe程序,弹出错误信息: libgcc_s_sjlj-1.dll is missing 百度了一下,原来是编 ...

  3. BOM编程

    1 BOM编程 1.1 入门 BOM就是浏览器对象模型编程,通过javascript引擎提供的四个浏览器对象,操作浏览器,这叫BOM编程 1.2 window对象(重点) window代表的是一个窗口 ...

  4. 可复用 React 的 HOC 以及的 Render Props

    重复是不可能的,这辈子都不可能写重复的代码 当然,这句话分分钟都要被产品(领导)打脸,真的最后一次改需求,我们烦恼于频繁修改的需求 虽然我们不能改变别人,但我们却可以尝试去做的更好,我们需要抽象,封装 ...

  5. Java分享笔记:使用keySet方法获取Map集合中的元素

    /*--------------------------- Map集合中利用keySet方法获取所有的元素值: ....keySet方法:将Map中的所有key值存入到Set集合中, ....利用Se ...

  6. MySQL运行一段时间后自动停止问题的排查

    在进入主题前,一定要先吐槽下自己,前段时间购买了一台阿里云服务器,最开始打算只是自己个人用的,就买了一台配置很寒碜的服务器: CPU: 1核 内存: 1 GB 操作系统: CentOS 7.2 64位 ...

  7. Eclipse搭建SpringBoot

    第一种方法(不建议) 首先新建Maven工程 勾选第一个按钮,第三个是选择working set ,你可以不选 下一步,配置工程信息,注意打包为jar 打开pom.xml文件,添加spring-boo ...

  8. PHP 日期处理函数 date() 、mktime()

    一.前言 php是世界上最好的语言! 二.介绍 mktime()函数获取当周\当天\当月 /** * 微程-日期工具函数 week: 当周 day: 当天 month: 当月 * @author 狗蛋 ...

  9. LAMP架构的搭建

    什么是LAMP架构? L : Linux,2.6.18-308.el5(redhat5.8) A :Apache,httpd 2.4.4 M :  mysql-5.5.28  P : php-5.4. ...

  10. 裸机——iNand

    1.先晓得iNand的基础知识 iNand是在SD卡基础上发展来的,而SD卡是在MMC的基础上发展来的,MMC是在Nand的基础上发展来的 我们晓得Nand的基础知识,而MMC对Nand大致做了两个改 ...