LightOJ1100 Again Array Queries 暴力

题目链接:

http://lightoj.com/volume_showproblem.php?problem=1100

1100 - Again Array Queries

   PDF (English)StatisticsForum
Time Limit: 3 second(s)Memory Limit: 32 MB

Given an array with n integers, and you are given two indices i and j (i ≠ j) in the array. You have to find two integers in the range whose difference is minimum. You have to print this value. The array is indexed from 0 to n-1.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case contains two integers n (2 ≤ n ≤ 105) and q (1 ≤ q ≤ 10000). The next line contains n space separated integers which form the array. These integers range in [1, 1000].

Each of the next q lines contains two integers i and j (0 ≤ i < j < n).

Output

For each test case, print the case number in a line. Then for each query, print the desired result.

Sample Input

Output for Sample Input

2

5 3

10 2 3 12 7

0 2

0 4

2 4

2 1

1 2

0 1

Case 1:

1

1

4

Case 2:

1

Notes

Dataset is huge, use faster I/O methods.


SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)

 题目大意:

T组数据

每组数据n个,q个询问

每次询问0<=l,r<n,区间[l,r]之内任意两个数的差绝对值的最小值

思路:

暴力+记录出现的个数,【1-1000】暴力

This is the code:

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
int a[100005];
int v[1005];
int main()
{
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;++t)
    {
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=0;i<n;++i)
            scanf("%d",a+i);
        printf("Case %d:\n",t);
        while(q--)
        {
            int l,r;
            int ans=INT_INF;
            int pre=-INT_INF;//处理第一个出现的数据
            scanf("%d%d",&l,&r);
            memset(v,0,sizeof(v));
            for(int i=l;i<=r;++i)
                v[a[i]]++;
            for(int i=1;i<=1000;++i)
            {
                if(!v[i])
                    continue;
                if(v[i]>=2)
                {
                    ans=0;
                    break;
                }
                ans=min(ans,i-pre);
                pre=i;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值