Killer Problem
发布日期:2021-09-19 10:55:58
浏览次数:2
分类:技术文章
本文共 1339 字,大约阅读时间需要 4 分钟。
You are given an array of N integers and Q queries. Each query is a closed interval [l, r]. You should
find the minimum absolute difference between all pairs in that interval.InputFirst line contains an integer T (T ≤ 10). T sets follow. Each set begins with an integer N (N ≤200000). In the next line there are N integers ai (1 ≤ ai ≤ 104), the number in the i-th cell of thearray. Next line will contain Q (Q ≤ 104). Q lines follow, each containing two integers li, ri (1 ≤ li,ri ≤ N, li < ri) describing the beginning and ending of of i-th range. Total number of queries will beless than 15000.OutputFor the i-th query of each test output the minimum |ajak| for li ≤ j, k ≤ ri (j ̸= k) a single line.Sample Input1101 2 4 7 11 10 8 5 1 1000041 101 23 58 10Sample Output0134
题目大概:
给出n个数,m条询问。求出l 到r 区间内任意一对数的差绝对值的最小值。
思路:
看了数据量,首先发现,数据的大小比较小。枚举一对对的数一定会超时,所以,可以枚举数的大小。而且数的大小是从最小值枚举到最大值,是排好序的,只需要算相邻的差的绝对值就好了。这样,时间复杂度就降低了不少,好像过很勉强,试一试,过了。
代码:
#includeusing namespace std;const int maxn=2e5+10;const int INF=0x3f3f3f3f;int a[maxn];int b[maxn];int work(int l,int r){ memset(b,0,sizeof(b)); int min_1=INF; int max_1=0; for(int i=l;i<=r;i++) { if(!b[a[i]])b[a[i]]++; else return 0; min_1=min(min_1,a[i]); max_1=max(max_1,a[i]); } int pre=min_1; //cout< <
转载地址:https://blog.csdn.net/a1046765624/article/details/80753052 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月07日 02时30分03秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Lua(十五)——协同程序
2019-04-27
Lua(十六)——文件
2019-04-27
Lua(十七)——面向对象
2019-04-27
Lua(十八)——错误处理,垃圾回收
2019-04-27
xLua(一)——介绍
2019-04-27
xLua(二)——下载
2019-04-27
xLua(三)——在C#中访问Lua代码
2019-04-27
xLua(四)——C#访问Lua的基本类型
2019-04-27
xLua(五)——C#访问Lua的table
2019-04-27
xLua(六)——C#访问Lua的function
2019-04-27
基础知识——常用单位介绍
2019-04-27
xLua(七)——Lua访问C#(一)
2019-04-27
xLua(八)——Lua访问C#(二)
2019-04-27
Unity中实现解析Json文件
2019-04-27
Unity自带Json解析库——JsonUtility
2019-04-27
Unity中使用ViedoPlayer操作视频文件
2019-04-27
C#中的的输入与输出
2019-04-27
C#中@符号的作用
2019-04-27
C#中$符号的作用
2019-04-27
Mac装windows系统后如何更换触控板设置
2019-04-27