1100 - Again Array Queries (鸽笼定理)
2015-11-25 11:47
477 查看
[align=center]1100 - Again Array Queries [/align]
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.
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).
题解: 鸽笼定理的灵活应用,因为数的范围是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;
}
PDF (English) | Statistics | Forum |
Time Limit: 3 second(s) | Memory Limit: 32 MB |
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;
}
相关文章推荐
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- 数据结构之Treap详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)
- java数据结构和算法学习之汉诺塔示例
- Java数据结构及算法实例:三角数字
- Java数据结构之简单链表的定义与实现方法示例
- 数据结构之AVL树详解
- qqwry.dat的数据结构图文解释第1/2页
- JavaScript中数据结构与算法(五):经典KMP算法