light oj 1100 - Again Array Queries
来源:互联网 发布:域名主机 编辑:程序博客网 时间:2024/05/22 02:13
http://lightoj.com/volume_showproblem.php?problem=1100
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
题意:给你n个数和q次查询,每次查询区间[i, j]里面所有数 两两间的最小差值。
思路:由于所有数范围(1 —— 1000)我们可以用cnt记录区间里面每个数出现的次数,然后从前向后扫一遍所有数,只要某个数出现次数大于2,最小差值就为0;出现次数为1,那么逐个更新。
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;#define INF 0x3f3f3f3f#define N 300000int val[N], cnt[1001];int main(){ int t, nCase = 1; scanf ("%d", &t); while (t--) { int n, q, a, b; scanf ("%d %d", &n, &q); for (int i=0; i<n; i++) scanf ("%d", &val[i]); printf ("Case %d:\n", nCase++); while (q--) { scanf ("%d %d", &a, &b); if (b - a + 1 > 1000) printf ("0\n"); else { int flag = 0, pre = 0, minx = INF; memset (cnt, 0, sizeof (cnt)); for (int i=a; i<=b; i++) cnt[ val[i] ]++; for (int i=1; i<=1000; i++) { if (flag) break; if (cnt[i] >= 2) { flag = 1; break; } else if (cnt[i] == 1) { if (!pre) pre = i; else { minx = min (minx, i-pre); if (!minx) { flag = 1; break; } pre = i; } } } if (flag) minx = 0; printf ("%d\n", minx); } } } return 0;}
- light oj 1100 - Again Array Queries
- Light oj 1082 - Array Queries 【RMQ 裸题】
- Light OJ 1082 Array Queries 【RMQ裸题】
- LightOJ 1100 - Again Array Queries
- Light oj 1082 - Array Queries(区间最小值)
- Light OJ 1082 1082 - Array Queries(区间最值)
- Light OJ-1082 Array Queries(线段树最值查询)
- Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~
- lightoj 1100 - Again Array Queries 【思维题】
- Lightoj 1100 - Again Array Queries (枚举剪枝)
- 1100 - Again Array Queries (鸽笼定理)
- lightoj1100 - Again Array Queries
- light oj 1369 - Answering Queries
- light oj 1082 - Array Queries【线段树】最简单的查询最值
- Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~
- Light OJ 1369 - Answering Queries 【规律】
- Light OJ 1369 Answering Queries (思维)
- Light OJ 1061 N Queen Again
- 快速索引 (对View的自定义,黑马程序员)
- C# DataGridView内容合并相同的行
- R绘图lattice包
- Codeforces Round #322 (Div. 2)
- Oracle基本用法
- light oj 1100 - Again Array Queries
- Android获取控件大小的方法
- 万维网WWW简介
- C语言使用正则表达式
- java简单的各种进制转换
- pygame编写的坦克游戏(七)
- [TwistedFate]OC面向对象
- win7+mingw+opengl+eclipse
- 高效使用你的Xcode