您的位置:首页 > 产品设计 > UI/UE

1100 - Again Array Queries (鸽笼定理)

2015-11-25 11:47 477 查看
[align=center]1100 - Again Array Queries [/align]



PDF (English)
Statistics
Forum
Time Limit: 3 second(s)Memory Limit: 32 MB
Given an array with n integers, and you are given twoindices
i
and j (i ≠ j) in the array. You have to find twointegers in the range whose difference is minimum. You have to print thisvalue. 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 spaceseparated integers which form the array. These integers range in
[1, 1000].

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

Output

For each test case, print the case number in a line. Thenfor 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.

题解: 鸽笼定理的灵活应用,因为数的范围是1~1000,所以如果查询的区间大于1000,则可以知道区间肯定有重复的数. 通过这个优化,就可以把区间查询的复杂度优化到1000以内.

AC代码:

/* ***********************************************
Author :xdlove
Created Time :2015年11月24日 星期二 20时01分38秒
File Name :lightoj/1100/Again_Array_Queries.cpp
************************************************ */

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

#define REP_ab(i,a,b) for(int i = a; i <= b; i++)
#define REP(i, n) for(int i = 0; i < n; i++)
#define REP_1(i,n) for(int i = 1; i <= n; i++)
#define DEP(i,n) for(int i = n - 1; i >= 0; i--)
#define DEP_N(i,n) for(int i = n; i >= 1; i--)
#define CPY(A,B) memcpy(A,B,sizeof(B))
#define MEM(A) memset(A,0,sizeof(A))
#define MEM_1(A) memset(A,-1,sizeof(A))
#define MEM_INF(A) memset(A,0x3f,sizeof(A))
#define MEM_INFLL(A) memset(A,0x3f3f,sizeof(A))
#define mid (((l + r) >> 1))
#define lson l, mid, u << 1
#define rson mid + 1, r, u << 1 | 1
#define ls (u << 1)
#define rs (u << 1 | 1)

typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 5;
const int MAXM = MAXN;
const int mod = 1e9 + 7;
int a[MAXN],num[1005];

inline void in(int &x) {
x = 0;
char ch = getchar();
while(!isdigit(ch)) ch = getchar();
while(isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
}

void work() {
int l,r,ans = INF;
in(l),in(r);
if(r - l >= 1000) { //鸽笼定理,区间范围大于1000,说明区间肯定有重复的.
ans = 0;
} else {
MEM(num);
REP_ab(i,l,r) num[a[i]]++;
int pre = -1;
REP_1(i,1000) {
if(ans == 0) break;
if(num[i] > 1) ans = 0;
else if(num[i] && pre == -1) pre = i;
else if(num[i]) ans = min(ans,i - pre),pre = i;
}
}
printf("%d\n",ans);
return;
}

int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,q,T,cnt = 0;
cin>>T;
while(T--) {
in(n),in(q);
REP(i,n) in(a[i]);
printf("Case %d:\n",++cnt);
while(q--) {
work();
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构