HDU-5289<two pointers>
题意:
求一个数列中存在多少个区间,每个区间内的数的差不超过k;
思路:two_pointers;
#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int N=1e5+5;
int n,a[N],k;
int main ()
{
int t; scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",a+i);
multiset<int>m;
long long ans=0;
for(int l=1,r=1;l<=n;l++)
{
while(r<=n)
{
if(m.size())
if(a[r]-*m.begin()>=k||*m.rbegin()-a[r]>=k)
break;
m.insert(a[r++]);
}
ans+=r-l;
m.erase(m.find(a[l]));
}
printf("%lld\n",ans);
}
return 0;
}
HDU-5289<two pointers>的更多相关文章
- HDU 5289 Assignment(二分+RMQ-ST)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 5289 Assignment [优先队列 贪心]
HDU 5289 - Assignment http://acm.hdu.edu.cn/showproblem.php?pid=5289 Tom owns a company and he is th ...
- HDU 5289
http://acm.hdu.edu.cn/showproblem.php?pid=5289 给一个数列,求有多少区间,使得这些区间内的最大值减最小值小于k 单调队列的功能:O(1) 插入,删除,最大 ...
- hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...
- HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is ...
- HDU 5289 Assignment rmq
Assignment 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Description Tom owns a company and h ...
- HDU 5289 Assignment (ST算法区间最值+二分)
题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...
- HDU 5289 尺取
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 5289 rmp+二分+枚举后界 or单调队列 ****
好题~~ 给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数,枚举后界~~ 又是一种没见过的方法,太弱了/(ㄒoㄒ)/~~ #include <cstdio ...
- HDU 5289 Assignment (数字序列,ST算法)
题意: 给一个整数序列,多达10万个,问:有多少个区间满足“区间最大元素与最小元素之差不超过k”.k是给定的. 思路: 如果穷举,有O(n*n)复杂度.可以用ST算法先预处理每个区间最大和最小,O(n ...
随机推荐
- 您可能无法使用服务器管理器,如果两个线程同时访问 IIS 管理 IIS 的修补程序
http://support.microsoft.com/kb/946517 如果多线程操作 win2003 iis 失败, 打上这个补丁就好了
- leetcode70
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- thinkphp的model模型的设计经验总结
关于模型:跟上篇文章thinkphp的目录结构设计经验总结写控制器一个道理:为了尽量避免改动到框架: 首先我们是要有一个BaseModel.class.php作为我们的基础model: 我会在Base ...
- 第四十四节,subprocess系统命令模块
用python执行系命令的相关的模块和函数的功能均在 subprocess 模块中实现,并提供了丰富的功能 call()模块函数 功能:输入系统命令,直接执行命令,返回状态码,只能查看[有参] 使用方 ...
- 文本输入框和下拉菜单特效-用正则表达式验证E-mail格式
———————————————————————————— <script type="text/javascript"> ...
- java fork-join框架应用和分析
http://shmilyaw-hotmail-com.iteye.com/blog/1897636 java fork-join框架应用和分析 博客分类: concurrency multithre ...
- hdu_5763_Another Meaning(dp)
题目链接:hdu_5763_Another Meaning 题意: 一个文本串A,一个模式串B,如果文本串含有模式串B,那么就能组合成多种意思,如下: In the first case, “ heh ...
- sort函数使用的基本知识
STL中就自带了排序函数sortsort 对给定区间所有元素进行排序 要使用此函数只需用#include <algorithm> sort即可使用,语法描述为:sort(begin,en ...
- C++primer 9.43
题目要求:编写一个函数,接受三个string参数s,oldVal和newVal.使用迭代器及insert和erase函数将s中所有oldVal替换为newVal.测试你的程序,用它替换通用的简写形式, ...
- listview条目用状态选择器没反应
button和imagebutton天生具有“可点击(click)”.“可按下(press)”的特性,radiobutton具有“可勾选(check)”的特性,但是listview的条目只有“可按下( ...