UVA - 11898 Killer Problem

Description

 Killer Problem 

You are given an array of N integers and
Q queries. Each query is a closed interval [l,
r]. You should find the minimum absolute difference between all pairs in that interval.

Input

First line contains an integer T (T10).
T sets follow. Each set begins with an integer
N (N200000). In the next line there are
N integers ai (1ai104),
the number in the i-th cell of the array. Next line will contain
Q (Q104).
Q lines follow, each containing two integers
li, ri (1li,
riN,
li < ri) describing the beginning and ending of of
i-th range. Total number of queries will be less than 15000.

Output

For the i-th query of each test output the minimum
| ajak| for
lij,
kri (jk)
a single line.

Sample Input

1
10
1 2 4 7 11 10 8 5 1 10000
4
1 10
1 2
3 5
8 10

Sample Output

0
1
3
4

题意:给定一个整数数组,对每组的询问L,R,输出区间差最小是多少

思路:排序后处理,但是这样会超时,因为题目说和不超过15000,所以最差也就只有15000个数

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 200005;
const int inf = 0x3f3f3f3f;

int num[maxn];
int tmp[maxn];
int n, q;

void cal(int l, int r) {
	if (r - l + 1 > 15000) {
		printf("0\n");
		return;
	}
	int cnt = 0;
	for (int i = l; i <= r; i++)
		tmp[cnt++] = num[i];
	sort(tmp, tmp+cnt);
	int Min = inf;
	for (int i = 0; i < cnt-1; i++)
		Min = min(Min, tmp[i+1]-tmp[i]);
	printf("%d\n", Min);
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
			scanf("%d", &num[i]);
		scanf("%d", &q);
		int l, r;
		while (q--) {
			scanf("%d%d", &l, &r);
			cal(l, r);
		}
	}
	return 0;
}

UVA - 11898 Killer Problem

时间: 2024-10-04 09:54:27

UVA - 11898 Killer Problem的相关文章

uva 11991 Easy Problem from Rujia Liu? Data Structure

// uva 11991 Easy Problem from Rujia Liu? // 给一个包含n个数的数组,需要回答若干个询问,每次询问两个整数 // k和v,输出从左到右第k个v的下标 // // 本题因为n比较大,所以直接开二维数组是不现实的 // 如果直接用vector的话,也是会有大量的浪费 // 所以可以先离散化一下,找到一共有多少个不同的数 // 再用vector动态数组就可以搞定了 // // 看着书上的map写的挺精彩的,我就习用了下来 // // 非常精彩,继续练吧...

uva 11991 - Easy Problem from Rujia Liu?(STL)

题目链接:uva 11991 - Easy Problem from Rujia Liu? 题目大意:给出一个包含n个整数的数组,你需要回答若干询问,每次询问两个整数k和v,输出从左到右第k个v的下标 解题思路:用map映射一个vector,对应即为map<int>即为一个可变长的数组,读取数组的时候将对应值放入即可. #include <cstdio> #include <cstring> #include <map> #include <vecto

UVA 10779 - Collectors Problem(网络流)

UVA 10779 - Collectors Problem 题目链接 题意:每个人有一种贴图,现在第0个人要去和别人交换贴图,来保证自己的贴图尽量多,只有别人没有该种贴图,并且自己有2张以上另一种贴图才会换,问最多有几张贴图 思路:最大流,关键在于如何建模,把0号人和物品连边,容量为有的容量,然后其他人如果物品等于0的,连一条边从物品到这个人,表示能交换,然后如果物品大于1的,连一条边从这个人到物品,容量为物品减1(自己要留一个),然后把所有物品连到汇点,跑一次最大流即可 代码: #inclu

uva 10104 Euclid Problem (数论-扩展欧几里德)

 Euclid Problem  The Problem From Euclid it is known that for any positive integers A and B there exist such integers X and Y that AX+BY=D, where D is the greatest common divisor of A and B. The problem is to find for given A and B corresponding X, Y

(DS 《算法入门经典》)UVA 11991 Easy Problem from Rujia Liu?(求第k个v出现的索引)

题目大意: 求第k个v出现的索引 解题思路: 如果能构造出一个数据结构,使得data[v][k]就是第k个v出现的索引值即可求解.data[v]表示数v出现的索引数组, data[v][k]表示第k个v出现的索引. Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 200

[UVA] 11991 - Easy Problem from Rujia Liu? [STL应用]

11991 - Easy Problem from Rujia Liu? Time limit: 1.000 seconds Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 2006, Beijing 2007 and Wuhan 2009, or UVa OJ con

UVA 11991 Easy Problem from Rujia Liu?【STL】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142 题意: 给一个长度n的序列,有m询问,每个询问会问第k个出现的数字的下标是多少 用map记录 代码: #include <stdio.h> #include <iostream> #include <algorithm> #includ

UVA 11991 Easy Problem from Rujia Liu? 简单STL

题目链接: UVA, 你懂的 题目描述: 给出一串数, 求第几次出现的数字几? 解题思路: 北交集训STL水题 代码: #include <iostream> #include <cstdio> #include <cstring> #include <map> using namespace std; const int MAXN = 1e5 + 7; map<int, int> cnt; map<pair<int, int>

UVA 10026-Shoemaker’s problem(贪心)

Shoemaker's Problem Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can work on only one job in each day. For each ithjob, it is known the integer Ti (1<=Ti<=1000), the time in days it takes the shoemaker to finish the job