CSU-1170 A Simple Problem
题目链接
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的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers(线段树/区间更新)
题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Description Yo ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- 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 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77964 Acc ...
随机推荐
- 将命令的输出生成一个Web页面
解决方法: ConvertTo-Html 命令: 生成一个HTML表格来代表命令的输出,为你提供的每个对象创建一行,在每行中,Powershell会创建代表对象属性的值. 实现效果:
- centos 7jenkin+git 安装
jenkins+git配置 背景:用git管理源代码,所以需要jenkins安装Git Plugin插件配置 准备: 1.linux环境git客户端 2.jenkins环境 + git plugin插 ...
- MCV 的几种表单提交方式
一,MVC HtmlHelper方法 Html.BeginForm(actionName,controllerName,method,htmlAttributes){} 其中actionName ...
- C# 操作符与表达式
C#保留了C++所有的操作符,其中指针操作符(*和->)与引用操作符(&)需要有unsafe的上下文.C#摈弃了范围辨析操作符(::),一律改为单点操作符(.).我们不再阐述那些保留的C ...
- matlab vs联调
vs 和matlab联调,最近真的把我搞挂了要. 首先,怎么进入联调呢.在vs里先设置一下. vs:tools->attach to process,选择matlab,注意此时matlab一定是 ...
- NodeJS基础入门
1.前端最主流的JavaScript运行环境 1>Node.js是一个基于Chrome V8引擎的JavaScript运行环境. 2>Node.js使用了一个事件驱动.非阻塞式I/O的模型 ...
- mysql 数据库设计规范
MySQL数据库设计规范 目录 1. 规范背景与目的 2. 设计规范 2.1 数据库设计 2.1.1 库名 2.1.2 表结构 2.1.3 列数据类型优化 2.1.4 索引设计 2.1.5 分库分表. ...
- Linq to SQL八大子句
查询数据库中的数据 from- in子句 指定查询操作的数据源和范围变量 select子句 指定查询结果的类型和表现形式 where子句 筛选元素的逻辑条件,一般由逻辑运算符组成 group- by子 ...
- JAVA / MySql 编程——第六章 Mysql 创建账户的相关命令
1. 创建普通用户: 语法: CREATE USER `user`@`host` [IDENTIFIED 'password']; //user:用户名,host:主机名,passw ...
- (转)零基础学习 Hadoop 该如何下手?
推荐一些Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeeper, Avro, Amb ...