题目链接

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. gearman安装实录

    花了5个小时装好了gearman,问题不断,坑爹的服务器yum还坏了,悲催. 服务器系统:centos5.3 64位 gearman版本:1.1.8 安装包(相关依赖)下载 1.gearman安装包 ...

  2. P1217 [USACO1.5]回文质数 Prime Palindromes

    题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...

  3. 20145238-荆玉茗 《Java程序设计》实验三

    20145238-荆玉茗-实验三 实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法应用到软件的开发.运营 ...

  4. 微信小程序清除默认样式

    1.清除button的默认样式 button::after{border:none;}input{outline:none;border:none;list-style: none;}

  5. C/C++获取本机名+本机IP+本机MAC

    本机名.IP.MAC都是一些比较常用网络参数,怎么用C/C++获取呢? 研究了两三个小时... 需要说明的都在代码注释里 #include <stdio.h> #include <W ...

  6. MappingException:class com.zsn.crm.Model.user not found whie looking for property user id

    之前好好地运行 什么东西都没动过 再次运行突然报异常*****MappingException:class com.zsn.crm.Model.user not found whie looking ...

  7. 基于mybatis设计简单信息管理系统---jsp页面

    1.在设计编辑界面的时候需要有一个下拉的列表页,想要他指定到指定的值: <select id="categoryId" name="categoryId" ...

  8. datatables 给字段设置默认值,屏蔽没有字段的错误

    我们返回的数据不能保证都是正常的,可能包含 null ,显然这个对于最终用户来说是不友好的,那么我们可以这么处理 先有如下数据格式: //示例数据 { data:[ {"id":1 ...

  9. thymeleaf单选回显,多选回显,选回显,下拉默认选中第一个

    //默认选中第一个<input type ="radio" name="repaymentType" th:each ="repaymentTy ...

  10. Shell学习——列出当前路径下所有目录

    1.ls -d */[root@client02 ~]# ls -d */Desktop/ Documents/ Downloads/ jq-1.5/ Music/ Pictures/ Public/ ...