lightoj Again Array Queries

1100 - Again Array Queries

  PDF (English) Statistics Forum
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

巧妙暴力:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 #define mem(x,y) memset(x,y,sizeof(x))
 8 const int INF=0x3f3f3f3f;
 9 const double PI=acos(-1.0);
10 const int MAXN=1e5+10;
11 int m[MAXN];
12 int cnt[1010];
13 void getans(int l,int r){
14     if(r-l>=1000){//因为数字范围1-1000;
15         puts("0");return;
16     }
17     mem(cnt,0);
18     for(int i=l;i<=r;i++)
19         cnt[m[i]]++;
20     int k=-1,ans=1000;
21     for(int i=1;i<=1000;i++){
22         if(cnt[i]>1){
23             ans=0;break;
24         }
25         if(cnt[i]){
26         if(k!=-1&&i-k<ans)ans=i-k;
27         k=i;
28         }
29     }
30     printf("%d\n",ans);
31 }
32 int main(){
33     int T,n,q,flot=0;
34     scanf("%d",&T);
35     while(T--){
36         scanf("%d%d",&n,&q);
37         for(int i=0;i<n;i++)scanf("%d",m+i);
38         printf("Case %d:\n",++flot);
39         while(q--){
40             int l,r;
41             scanf("%d%d",&l,&r);
42             getans(l,r);
43         }
44     }
45     return 0;
46 }
时间: 2024-07-30 13:45:59

lightoj Again Array Queries的相关文章

LightOJ Array Queries 1082【线段树求区间最值】

1082 - Array Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Given an array with N elements, indexed from 1 to N. Now you will be given some queries in the form I J, your task is to find the minimum value from index

lightoj-1082 - Array Queries

1082 - Array Queries PDF (English) Statistics ForumTime Limit: 3 second(s) Memory Limit: 64 MBGiven an array with N elements, indexed from 1 to N. Now you will be given some queries in the form I J, your task is to find the minimum value from index I

Array Queries CodeForces - 797E

Array Queries CodeForces - 797E WATLERE之路: 很显然的一道dp题,于是我妄想着通过时间O(n^2)的dp把它A掉,然后,我就走上了WATLERE之路..... 第一次,递归,RE 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int ans[100100]; 6 int a[100100]; 7 int n

codeforces 797 E. Array Queries【dp,暴力】

题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为止,求每次询问要转换的次数. 题解:纯暴力会TLE,所以在k为根号100000范围内dp打表 dp[i][j]表示初始p为i, k为j,需要转换几次可以大于n. 状态转移方程:dp[i][j] = dp[i+a[i]+j] + 1 #include <cstdio> #include <al

LightOJ 1188 Fast Queries(简单莫队)

1188 - Fast Queries    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Given an array of N integers indexed from 1 to N, and q queries, each in the form i j, you have to find the number of distinct integers from index i to 

LightOJ 1164 - Horrible Queries(线段树啊 功能:区间增减和区间求和)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1164 World is getting more evil and it's getting tougher to get into the Evil League of Evil. Since the legendary Bad Horse has retired, now you have to correctly answer the evil questions of Dr.

Light oj 1100 - Again Array Queries (鸽巢原理+暴力)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1100 给你n个数,数的范围是1~1000,给你q个询问,每个询问问你l到r之间数的最小差是多少. 要是l到r的数字个数大于1000,必定会有两个数字重复,所以此时最小差就为0.要是小于等于1000就直接暴力即可. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <alg

CodeForcs 797E Array Queries

$dp$预处理,暴力. 如果$k > sqrt(n)$,那么答案不会超过$sqrt(n)$,暴力模拟即可.如果$k <= sqrt(n)$,那么可以$dp$预处理打表. #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <queue> #include <stack&

863D - Yet Another Array Queries Problem(思维)

原题连接:http://codeforces.com/problemset/problem/863/D 题意:对a数列有两种操作: 1 l r ,[l, r] 区间的数字滚动,即a[i+1]=a[i], a[l]=a[r] 2 l r ,[l, r] 区间的数字位置反转. 若干个操作之后输出a[b[i]]. 思路: 由于是在操作结束后输出,且b[i]的个数不多(<=100),所以可以通过反推求出答案. AC代码: 1 #include<iostream> 2 #include<cs