Killer Problem

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.
Input
First 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 the
array. 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 be
less than 15000.
Output
For the i-th query of each test output the minimum |ajak| for li ≤ j, k ≤ ri (j ̸= k) a single line.
Sample Input
1
10
1 2 4 7 11 10 8 5 1 10000
4
1 10
1 2
3 5
8 10
Sample Output
0
1
3

4


题意:给定一个整数数组,对每组的询问L,R,输出区间差最小是多少

做的时候TLE超时了,大概是用的方法太过于暴力了,后来改了改总算是过了

#include<bits/stdc++.h>
#define maxn 200010
using namespace std;
long long a[maxn];
bool vis[10010];
int main(){
    long long t,i,j,n,m,l,r;
    bool flag;
    scanf("%lld",&t);
    while (t--){
        scanf("%lld",&n);
        for (i=1;i<=n;i++)
        scanf("%lld",&a[i]);
        scanf("%lld",&m);
        while (m--){
        scanf("%lld%lld",&l,&r);
        if (r-l+1>10000){
                puts("0");
                continue;
            }
            memset (vis,0,sizeof(vis));
            flag=0;
            for (i=l;i<=r;i++){
                if (!vis[a[i]]){
                    vis[a[i]]=1;
                }
                else {
                    flag=1;
                }
            }
            if (flag){
                puts("0");
            }
            else {
                long long temp=-1;
                long long minn=10000;
                for (i=1;i<=10000;i++){
                    if (vis[i]&&temp==-1){
                        temp=i;
                    }
                    else if (vis[i]){
                        long long fun=i-temp;
                        temp=i;
                        if (fun<minn)minn=fun;
                    }
                }
                printf("%lld\n",minn);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值