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 ...
随机推荐
- Edmonds-Karp算法,最大流POJ(1459)
题目链接:http://poj.org/problem?id=1459 解题报告: 电力调度站不涉及流的产生和消耗,不用考虑,Edmonds-Karp算法,就是利用剩余网络和增广路来解决,网络中的最大 ...
- 2、SpringBoot+Mybatis整合------一对一
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/93398da60c647573645917b2 ...
- 修改与遍历MAP
package com.*; import java.util.*; /** * Created by HP on 2018/8/2. */ public class Test { //定义一个用于转 ...
- java基础 File 递归删除文件夹中所有文件文件夹 目录(包含子目录)下的.java文件复制到e:/abc文件夹中, 并统计java文件的个数
File 递归删除文件夹中所有文件文件夹 package com.swift.kuozhan; import java.io.File; import java.util.Scanner; /*键盘录 ...
- jquery 操作ajax 相关方法
jQuery.get() 使用一个HTTP GET 请求从服务器加载数据. jQuery.get(url [,data] [,success(data,textStatus,jqXHR)] [dtaT ...
- 线段树的应用xx中学模拟lites
跟昨天那个自己写的,没有按照模板来的一看风格就不相类似,今天模拟赛的时候就是用的我的那个自己YY的代码,才拿了10分.个人认为关键的问题应该在于对于数据的处理太过繁琐了,所以回来之后,就拿了大佬的程序 ...
- Linux添加swap分区
swap分区的作用为当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用,那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到S ...
- elasticsearch-dsl笔记
一.elasticsearch安装 安装java1.8以上 安装elasticsearch-rtf(https://github.com/medcl/elasticsearch-rtf) head插件 ...
- linux学习笔记二:三种网络配置
本文引用自:https://www.linuxidc.com/Linux/2017-05/144370.htm [linux公社] VMware为我们提供了三种网络工作模式,它们分别是:Bridged ...
- 谷歌浏览器如何调试JS
平常在开发过程中,经常会接触到前端页面.那么对于js的调试那可是家常便饭,谷歌浏览器是常用来调试JS代码的工具,本文主要介绍如何利用谷歌浏览器来调试JS代码,协助我们进行开发工作,加快开发效率. 1. ...